Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Module build failed: SyntaxError: Unexpected token m #13

Closed
cheshire137 opened this issue Dec 25, 2015 · 18 comments
Closed

Module build failed: SyntaxError: Unexpected token m #13

cheshire137 opened this issue Dec 25, 2015 · 18 comments

Comments

@cheshire137
Copy link

This is what I'm getting:

ERROR in ./src/components/config.json
Module build failed: SyntaxError: Unexpected token m
    at Object.parse (native)
    at Object.module.exports (/Users/me/my-project/node_modules/json-loader/index.js:7:48)
 @ ./src/components/Main.js 28:13-37

The relevant JSON file I'm trying to load:

{
  "redirectUri": "http://localhost:8000/webpack-dev-server/"
}

I'm requiring it via var Config = require('./config.json');. I had the same error when I did require('json!./config.json').

I have this in my list of loaders:

{
  test: /\.json$/,
  loader: 'json-loader!json-loader'
}

I got the same error when I moved the loader to the preLoaders section.

This is the version I'm using: "json-loader": "^0.5.4".

@cheshire137
Copy link
Author

Looks like a duplicate of #10 that may have been fixed by #11 but 11 got closed instead of merged.

@cheshire137
Copy link
Author

I ended up using raw-loader and loading my file like this: var Config = JSON.parse(require('components/config.json')); The following is in my loaders section:

{
  test: /\.json$/,
  loader: 'raw-loader'
}

@thebarndog
Copy link

@cheshire137 I ran into the same issue today and for me at least, it was because I wasn't escaping double quotes like

{
    "text": "This is some example \"text\""
}

I also ran into an issue with using ES6 import syntax and eslint-import-plugin, it couldn't find the default export for josn but if you use require you should be a-ok.

@vinayrajp
Copy link

I ran into the same issue as well with json-loader.
@cheshire137 I also ran into this issue with raw-loader.
var Config = require('components/config.json'));
would return module.exports = { json data } and when that was passed to JSON.parse, I got the same Unexpected token m (since the file starts with m). Not sure why just the data within the file was not returned with raw-loader.

@mindmelting
Copy link

Can confirm #11 fixes this issue...

@nhll
Copy link

nhll commented Feb 5, 2016

I got the same error. Any news on this issue?

Can also confirm #11 fixes this, why didn't it get merged?

@salsa2k
Copy link

salsa2k commented Feb 5, 2016

I have the same issue

@Martskin
Copy link

Can confirm #11 fixes this issue...

Yeah, #11 fixes it for me too.

@sokra
Copy link
Member

sokra commented Feb 24, 2016

This issue occurs when applying the json-loader twice. i. e. by using configuration + require("json!. That's wrong. #11 is only a workaround, but not the solution. Fix your configuration resp. code.

@Martskin
Copy link

Thanks @sokra ! Removing require("json! did the trick.

@Shinobi881
Copy link

In case anyone is still having an issue with this loader, I got it to work by adding and extra ! to my import/require statement.

Ex: import Data from '!json!../../data';
And the loader: { test: /\.json$/, loaders: ['json-loader'] }

Shinobi881 pushed a commit to Shinobi881/json-loader that referenced this issue Apr 22, 2016
Not sure if this is the only option but it worked with the following code:

- `import/require` - `import Data from '!json!../../data';`
- Loader: `{ test: /\.json$/, loaders: ['json-loader'] }`
- From: https://github.com/webpack/webpack/tree/master/examples/loader
@mattdell
Copy link

mattdell commented May 9, 2016

I fixed this issue by moving json-loader from loaders into preLoaders.

@demidovVladimirAuto1
Copy link

When #11 will be merged to not checkout git repo?

@gibo
Copy link

gibo commented Jun 17, 2016

+1

@sokra
Copy link
Member

sokra commented Jul 7, 2016

#13 (comment)

@AlastairTaft
Copy link

AlastairTaft commented Aug 31, 2016

Still can't get it to work despite trying all of the above recommendations. Doesn't even work using raw-loader.

I have this which doesn't work

{
  "schemaVersion": "2"
}

It throws this error message:
ERROR in ./~/json-loader!./src/interface.json
Module build failed: SyntaxError: Unexpected token   in JSON at position 2
at Object.parse (native)
at Object.module.exports

and then if I re-type all that stuff out manually to be exactly the same it works

{
  "schemaVersion": "2"
}

Seriously confused, I thought it might be encoding, so I saved my file as UTF8 but still doesn't work

UPDATE: Weird whitespace was the culprit

I searched for whitespace in my text editor, as you can see in the below screenshot there isn't any whitespace that makes up the indent on line 2? What the hell is this then?

problem json

But after I deleted that manually so now my whitespace search looks like this it works!
json that works

Wasted most of the day thinking it was something to do with json loaders, etc, etc. Pretty frustrating, maybe this will help somebody else out.

@ghost
Copy link

ghost commented Oct 14, 2016

Switching to fork compact-json-loader helped me :)

@h-cow
Copy link

h-cow commented Oct 18, 2016

I had to remove the "json!" and then it worked.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests