【问题标题】:Webpack not compiling - Module build failedWebpack 未编译 - 模块构建失败
【发布时间】:2017-05-03 05:54:10
【问题描述】:

我只是为我的 react.js 应用程序 设置我的开发环境,我遇到了 webpack 拒绝编译的错误。我收到错误 Module build failed: SyntaxError: Unexpected token at line 5。第 5 行是render (<App />, document.getElementById('app'));。我已将<script type="text/babel>" 添加到我的 index.html 上的脚本标记中,但我仍然遇到同样的错误。

这是我的 webpack.config.js:

var webpack = require('webpack');
var path = require('path');

module.exports = {
  devtool: 'inline-source-map',
  entry: [
      'webpack-dev-server/client?http://127.0.0.1:8080/',
      'webpack/hot/only-dev-server',
      './src'
  ],
  output: {
      path: path.join(__dirname, 'public'),
      filename: 'bundle.js'
  },
  resolve: {
      modules: ['node_modules', 'src'],
      extensions: ['.js', '.jsx']
},  
module: {
    rules: [
    {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        use: ['react-hot-loader', 'babel-loader']
    }
    ]
},

plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin()

};

我的app.js:

import React from 'react';


export default class App extends React.Component {
  render() {
      return (
          <div>
              <h1>My React App</h1>
          </div>

      );
   }
}

还有我的 index.js

import React from 'react';
import { render } from 'react-dom';
import App from './components/app';

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

我做错了什么?

【问题讨论】:

  • 你能显示你的 .babelrc 文件中有什么吗?

标签: javascript reactjs webpack babeljs


【解决方案1】:

你可以尝试添加一个 .babelrc 文件吗:

{
  "presets": [
    "es2015",
    "react"
  ]
}

然后做:

npm i --save-dev babel-preset-es2015
npm i --save-dev babel-preset-react

确保您也安装了babel-loader

【讨论】:

  • 谢谢。这有效!,从某种意义上说,错误不再存在。 (虽然它最初抛出了一个错误,因为你错过了花括号)。但是屏幕上什么也没有显示。
  • 修复了这个问题。我已经注释掉了插件。删除了评论标签,它的工作原理。请编辑您的答案并在数组周围添加花括号。同时删除方括号末尾的逗号。
猜你喜欢
  • 2019-11-06
  • 2016-02-10
  • 1970-01-01
  • 2021-05-07
  • 2019-07-05
  • 2017-10-18
  • 2017-09-30
  • 2017-10-07
相关资源
最近更新 更多