【问题标题】:While compiling my react app I am getting Module build failed: SyntaxError: Unexpected token在编译我的反应应用程序时,模块构建失败:SyntaxError: Unexpected token
【发布时间】:2020-02-20 09:58:44
【问题描述】:

我在下面附上了所需的代码,如果有人可以帮助我,我将不胜感激。

我的 app.js 是:

import React from 'react';
import ReactDOM from 'react-dom';
import IndecisionApp from './components/IndecisionApp';
import 'normalize.css/normalize.css';
import './styles/styles.scss';

ReactDOM.render(<IndecisionApp />, document.getElementById('app'));

错误是:

 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/app.js
webpack: Failed to compile.
webpack: Compiling...
Hash: 66aec74626cbf614b013
Version: webpack 3.1.0
Time: 22ms
  [84] ./src/app.js 691 bytes {0} [built] [failed] [1 error]
    + 84 hidden modules

ERROR in ./src/app.js
Module build failed: SyntaxError: Unexpected token (7:16)

  5 | import './styles/styles.scss';
  6 | 
> 7 | ReactDOM.render(<IndecisionApp />, document.getElementById('app'));
    |                 ^
  8 | 

 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/app.js
webpack: Failed to compile.

完整代码链接:https://github.com/bhatvikrant/IndecisionApp

最新错误信息:

ERROR in ./src/app.js
Module build failed: Error: Requires Babel "^7.0.0-0", but was loaded with "6.25.0". If you are sure you have a compatible version of @babel/core, it is likely that something in your build process is loading the wrong version. Inspect the stack trace of this error to look for the first entry that doesn't mention "@babel/core" or "babel-core" to see what is calling Babel. (While processing preset: "/Users/VIKRANT/Desktop/Indecision App/node_modules/@babel/preset-env/lib/index.js")
    at throwVersionError (/Users/VIKRANT/Desktop/Indecision App/node_modules/@babel/helper-plugin-utils/lib/index.js:65:11)
    at Object.assertVersion (/Users/VIKRANT/Desktop/Indecision App/node_modules/@babel/helper-plugin-utils/lib/index.js:13:11)
    at /Users/VIKRANT/Desktop/Indecision App/node_modules/@babel/preset-env/lib/index.js:177:7
    at /Users/VIKRANT/Desktop/Indecision App/node_modules/@babel/helper-plugin-utils/lib/index.js:19:12
    at /Users/VIKRANT/Desktop/Indecision App/node_modules/babel-core/lib/transformation/file/options/option-manager.js:317:46
    at Array.map (<anonymous>)
    at OptionManager.resolvePresets (/Users/VIKRANT/Desktop/Indecision App/node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
    at OptionManager.mergePresets (/Users/VIKRANT/Desktop/Indecision App/node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
    at OptionManager.mergeOptions (/Users/VIKRANT/Desktop/Indecision App/node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
    at OptionManager.init (/Users/VIKRANT/Desktop/Indecision App/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
    at File.initOptions (/Users/VIKRANT/Desktop/Indecision App/node_modules/babel-core/lib/transformation/file/index.js:212:65)
    at new File (/Users/VIKRANT/Desktop/Indecision App/node_modules/babel-core/lib/transformation/file/index.js:135:24)
    at Pipeline.transform (/Users/VIKRANT/Desktop/Indecision App/node_modules/babel-core/lib/transformation/pipeline.js:46:16)
    at transpile (/Users/VIKRANT/Desktop/Indecision App/node_modules/babel-loader/lib/index.js:49:20)
    at Object.module.exports (/Users/VIKRANT/Desktop/Indecision App/node_modules/babel-loader/lib/index.js:174:20)
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/app.js
webpack: Failed to compile.

【问题讨论】:

  • 你能添加你的Indecision App代码
  • @Vikrant Bhat 请分享您的 IndecisionApp 代码
  • 请分享您的webpack.config。您很可能忘记添加'@babel/preset-react'
  • 编译你的JSX有问题,webpack无法识别。可能你错过了在 webpack.config.js 中设置 babel,所以请与我们分享这个文件。
  • 我已将github链接添加到repo,请看一下

标签: reactjs webpack jsx webpack-dev-server


【解决方案1】:

您没有配置您的babel。有两种方法可以解决这个问题:

1) 更新您的 webpack.config:

//...
{
    test: /\.js$/,
    exclude: /node_modules/,
    use: {
        loader: 'babel-loader',
        options: {
            /**
             * Use modules: false, otherwise hot-reloading will be broken
             */
            presets: [
                '@babel/preset-env',
                '@babel/preset-react'
            ],
        }
    }
},
//...

2) 或者在根目录下创建.babelrc文件(一般推荐),如下:

{
  "presets": ["@babel/preset-env", "@babel/preset-react"]
}

另外别忘了安装 babel 依赖:

npm install @babel/core babel-loader @babel/preset-env @babel/preset-react --save-dev

【讨论】:

  • 还有第三种方式,你可以将"babel"部分添加到你的package.json中,内容相似(例如,如果你不想创建额外的.babelrc文件)。
  • @Arseniy-II 根目录是src文件夹还是public文件夹?抱歉问了这么一个基本问题,我现在还是个新手
  • @VikrantBhat 这是您的webpack.config.jspackage.json 所在的文件夹
  • 我做了你建议的第二种方法,仍然得到一些新的错误:
  • @VikrantBhat 现在工作正常吗?或者你还有其他障碍?
猜你喜欢
  • 1970-01-01
  • 2017-12-06
  • 2016-04-09
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多