在编译我的应用程序时,我得到模块构建失败:SyntaxError:意外的令牌

时间:2019-10-24 05:57:51

标签: reactjs webpack jsx webpack-dev-server

The error that I am facing after doing the 2nd approach mentioned below我在下面附加了必需的代码,如果有人可以帮助我,我将不胜感激。

我的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.

1 个答案:

答案 0 :(得分: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