【问题标题】:Module parse failed: Unexpected token using webpack-dev-server模块解析失败:使用 webpack-dev-server 的意外令牌
【发布时间】:2018-10-11 06:03:51
【问题描述】:

我正在尝试遵循Learning Redux 书中的示例。不幸的是,最新版本的 webpack 和 babel 都发生了变化,我更新到了与书中配置不同的最新版本。

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

但我正在努力让 jsx 与 webpack-dev-server 一起编译。运行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"
            }
        }]
    }
}

感谢任何帮助。

【问题讨论】:

标签: reactjs webpack babeljs


【解决方案1】:

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

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

其他一切都应该工作。

【讨论】:

  • ermagod...我的阅读障碍...呃。谢谢。
  • 混淆文件名如何导致内容错误?没有给定任何配置文件时,webpack 的处理过程是什么?
  • @Titou webpack 如果找不到配置文件,则使用默认配置。默认路径是src/index.js,所以它找到了文件。但是默认配置没有使用 babel 编译,所以它无法读取 jsx
猜你喜欢
  • 2019-03-31
  • 2020-11-20
  • 2020-11-20
  • 2021-11-29
  • 2021-11-26
  • 2019-02-15
  • 2023-03-27
  • 1970-01-01
  • 2021-04-26
相关资源
最近更新 更多