简体   繁体   English

使用Webpack和ReactJS解决错误

[英]Resolving errors with Webpack and ReactJS

I've read lots of other S/O posts that detail a similar problem, however I cannot find a solution for my specific issue. 我已经阅读了很多其他S / O帖子,详细说明了类似的问题,但是我无法找到针对我的具体问题的解决方案。

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 这是我的Webpack配置

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. 注意:我安装了es2015,并且我尝试过多次重写webpack.config.js无济于事。

What am I doing incorrectly? 我做错了什么?

It is trying to load a .json file. 它正在尝试加载.json文件。 You currently do not have a json-loader setup to handle this sort of thing. 您目前没有json-loader设置来处理这类事情。 Have a look at json-loader . 看看json-loader

Example

npm install --save-dev json-loader

webpack.config.js webpack.config.js

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

      query: {
        presets: ['es2015', 'react']
      }
    },
    {
      test: /\.json$/,
      loader: 'json'
    }
  }
]
...

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 index.js:641 ./~/syllable/problematic.json模块解析失败:/Desktop/App/node_modules/syllable/problematic.json意外的令牌(2:11)你可能需要一个合适的加载器来处理这个文件类型

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 json-loader 你需要什么

install json-loader with npm and add it to your config. 使用npm安装json-loader并将其添加到您的配置中。

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

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 ? 如果您正在尝试测试.js和.jsx文件,请删除最后一个$并将其替换为? and remove the | 并删除| .

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM