模块解析失败:使用webpack-dev-server的意外令牌

时间:2018-05-01 06:50:17

标签: reactjs webpack babeljs

我试图按照Learning Redux一书中的示例进行操作。不幸的是,最新版本的webpack和babel都发生了变化,我更新到了与本书配置不同的最新版本。

我已经阅读了babel和webpack的最新文档,我相信我有正确的.babelrc和config.webpack.js是合适的。

但是我很难通过webpack-dev-server来编译jsx。运行npm start时出现此错误:

ERROR in ./src/index.js 
Module parse failed: Unexpected token (10:4) 
You may need an appropriate loader to handle this file type. 
| 
| ReactDOM.render( 
|     <h1>hello world!</h1>, 
|   document.getElementById('root') 
| )  
@ multi (webpack)-dev-server/client?http://localhost:8080 ./src

以下是我的完整设置:https://github.com/homanchou/learning_redux

的package.json

{
 "name": "learningredux",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "scripts": {
    "start": "webpack-dev-server --open --mode development",
    "build": "webpack --mode production",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.4",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "eslint": "^4.19.1",
    "eslint-plugin-react": "^7.7.0",
    "webpack": "^4.6.0",
    "webpack-cli": "^2.1.2",
    "webpack-dev-server": "^3.1.3"
  },
  "dependencies": {
    "npm": "^6.0.0",
    "react": "^16.3.2",
    "react-dom": "^16.3.2",
    "redux": "^4.0.0"
  }
}

babelrc。

{
 "presets": ["env", "react"],
 "plugins": [ "transform-object-rest-spread" ]
}

config.webpack.js

const path = require('path')
module.exports = {
    entry: './src/index.js',
    output: {
        path: path.resolve('dist'),
        filename: 'main.js'
    },
    module: {
        rules: [{
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
              loader: "babel-loader"
            }
        }]
    }
}

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您只是混合了config.webpack.js文件名中的顺序。 它必须是webpack.config.js

或者您需要使用--config运行webpack并传递文件名。

其他一切都应该有用。