webpack-contrib / json-loader

json loader module for webpack - UNMAINTAINED

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Module build failed: SyntaxError: Unexpected token m

cheshire137 opened this issue · comments

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".

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

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'
}

@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.

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.

Can confirm #11 fixes this issue...

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

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

I have the same issue

Can confirm #11 fixes this issue...

Yeah, #11 fixes it for me too.

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.

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

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'] }

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

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

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.

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

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