1

I've read lots of other S/O posts that detail a similar problem, however I cannot find a solution for my specific issue.

I installed a node module, but I am now receiving this error message:

(syllable - is the module I installed)

index.js:641 ./~/syllable/problematic.json
Module parse failed: /Desktop/App/node_modules/syllable/problematic.json Unexpected token (2:11)
You may need an appropriate loader to handle this file type.

Here is my Webpack config

var config = {
    entry: './main.js',

    output: {
      path: './',
      filename: 'index.js',
    },

    devServer: {
      inline: true,
      port: 3000
    },

    module: {
      loaders: [
        {
          test: /\.jsx$/,
          exclude: /node_modules/,
          loader: 'babel',
          query: {
            presets: ['es2015', 'react']
        },
        {     //<-- this line is throwing an unexpected token error
          test: /\.json$/,
          loader: 'json'
        }
      }
    ]
  }
}

module.exports = config;

Note: I have es2015 installed, and I have tried re-writing the webpack.config.js several times to no avail.

What am I doing incorrectly?

3 Answers 3

3

It is trying to load a .json file. You currently do not have a json-loader setup to handle this sort of thing. Have a look at json-loader.

Example

npm install --save-dev json-loader

webpack.config.js

...
module: {
  loaders: [
    {
      test: /\.jsx$/,
      exclude: /node_modules/,
      loader: 'babel',

      query: {
        presets: ['es2015', 'react']
      }
    },
    {
      test: /\.json$/,
      loader: 'json'
    }
  }
]
...
Sign up to request clarification or add additional context in comments.

5 Comments

json-loader was installed, however I forgot to add it to the configuration. I have now updated the webpack config as you suggested, but I'm still receiving the same error.
I edited my answer to replace the | with a \ to escape the . in your babel loader. Make sure to restart your webpack builder after changing your config. Is it still throwing the same error?
I am now getting an unexpected token error, even though the syntax appears correct.. I have updated my webpack.config.js above as per your instructions.
That's because we missed a } after the query key (right after the presets). See updated answer.
Also had to change the test: /\.jsx$/ to test: /\.jsx?$/ because it wasn't parsing properly. None the less, all fixed. Hurrah!
0

What is error and message

index.js:641 ./~/syllable/problematic.json Module parse failed: /Desktop/App/node_modules/syllable/problematic.json Unexpected token (2:11) You may need an appropriate loader to handle this file typ

And answer

You should really read it, It clearly says that Module parse failed. It gave you file name and told you that You may need an appropriate loader to handle this file typ

json-loader what you need

install json-loader with npm and add it to your config.

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

Comments

0

Currently your regular expression does not match any file extension. If you're trying to test both .js and .jsx file remove the last $ and replace it with ? and remove the |.

module:{
    loaders: [
        {
            test: /\.jsx?/,
            exclude: /node_modules/,
            loader: 'babel',

            query: {
                presets: ['es2015', 'react']
            }
        }
    ]
}

1 Comment

Your suggested regex is not correct (/.jsx?/). It would match files files ending with ajsx or 0jsx. You have to escape the . like this \.jsx (Add ? for optional x.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.