简体   繁体   English

Webpack抛出JSX语法错误

[英]Webpack throws syntax error for JSX

When I attempted to use webpack to compile my react jsx code, I received the following error: 当我尝试使用webpack编译我的react jsx代码时,收到以下错误:

ERROR in ./client/index.js
Module parse failed: C:\Users\Gum-Joe\Documents\Projects\bedel/client\index.js Unexpected token (6:11)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (6:11)
    at Parser.pp.raise (C:\Users\Gum-Joe\Documents\Projects\bedel\node_modules\acorn\dist\acorn.js:920:13)
    at Parser.pp.unexpected (C:\Users\Gum-Joe\Documents\Projects\bedel\node_modules\acorn\dist\acorn.js:1483:8)
    at Parser.pp.parseExprAtom (C:\Users\Gum-Joe\Documents\Projects\bedel\node_modules\acorn\dist\acorn.js:330:12)
    at Parser.pp.parseExprSubscripts (C:\Users\Gum-Joe\Documents\Projects\bedel\node_modules\acorn\dist\acorn.js:225:19)
    at Parser.pp.parseMaybeUnary (C:\Users\Gum-Joe\Documents\Projects\bedel\node_modules\acorn\dist\acorn.js:204:17)
    at Parser.pp.parseExprOps (C:\Users\Gum-Joe\Documents\Projects\bedel\node_modules\acorn\dist\acorn.js:151:19)
    at Parser.pp.parseMaybeConditional (C:\Users\Gum-Joe\Documents\Projects\bedel\node_modules\acorn\dist\acorn.js:133:19)
    at Parser.pp.parseMaybeAssign (C:\Users\Gum-Joe\Documents\Projects\bedel\node_modules\acorn\dist\acorn.js:110:19)
    at Parser.pp.parseExpression (C:\Users\Gum-Joe\Documents\Projects\bedel\node_modules\acorn\dist\acorn.js:86:19)
    at Parser.pp.parseReturnStatement (C:\Users\Gum-Joe\Documents\Projects\bedel\node_modules\acorn\dist\acorn.js:1854:26)
    at Parser.pp.parseStatement (C:\Users\Gum-Joe\Documents\Projects\bedel\node_modules\acorn\dist\acorn.js:1719:19)
    at Parser.pp.parseBlock (C:\Users\Gum-Joe\Documents\Projects\bedel\node_modules\acorn\dist\acorn.js:1991:21)
    at Parser.pp.parseFunctionBody (C:\Users\Gum-Joe\Documents\Projects\bedel\node_modules\acorn\dist\acorn.js:607:22)
    at Parser.pp.parseMethod (C:\Users\Gum-Joe\Documents\Projects\bedel\node_modules\acorn\dist\acorn.js:576:8)
    at Parser.pp.parseClassMethod (C:\Users\Gum-Joe\Documents\Projects\bedel\node_modules\acorn\dist\acorn.js:2137:23)
    at Parser.pp.parseClass (C:\Users\Gum-Joe\Documents\Projects\bedel\node_modules\acorn\dist\acorn.js:2122:10)
 @ ./client/index.js 1:0-20

.babelrc: .babelrc:

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

webpack.config.js: webpack.config.js:

// Webpack config
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {

  // Use client as our root
  context: __dirname + "/client",
  // Entry file
  entry: "./index.js",
  // Resolve
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  // Output to /build
  output: {
        path: path.join(__dirname, "build", "js"),
        filename: "bundle.js"
    },
  loaders: [
    { test: /\.jsx$/, exclude: /node_modules/, loader: "babel-loader" }
  ],
  // Plugins
  plugins: [
    // HTML
    new HtmlWebpackPlugin({
      title: 'Bedel',
      filename: path.join(__dirname, 'views', 'index.ejs'),
      template: path.join(__dirname, 'client', 'templates', 'index.ejs')
    })
  ]
};

index.js: index.js:

import React from 'react';
import ReactDOM from 'react-dom';

class App extends React.Component {
  render () {
    return <p> Hello React</p>;
  }
}

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

I have installed all the babel presets required, as well as babel-core . 我已经安装了所需的所有babel预设以及babel-core I have looked at the following answers already: 我已经看过以下答案:

Edit: After commenting out my jsx syntax, the outputting bundle.js does not appear to have been transformed by babel (ie I can see ES6 code in it) 编辑:注释掉我的jsx语法后,输出的bundle.js似乎没有被babel转换(即,我可以在其中看到ES6代码)

Edit: Sorry for the inconvenience, but app.jsx was a solution that I tried that involved putting the logic that should be in index.js into a separate file. 编辑:很抱歉给您带来不便,但是app.jsx是我尝试过的一种解决方案,涉及将应将index.js的逻辑放入单独的文件中。

Edit: Here is a list of the solutions I tried that did not work: 编辑:这是我尝试不起作用的解决方案的列表:

  • Copy .babelrc to client/.babelrc .babelrc复制到client/.babelrc
  • Change test to test for .js instead of .js 更改测试以测试.js而不是.js
  • Separate app logic into separate file ( app.js ) 将应用程序逻辑分为单独的文件( app.js
  • Put presets to use in webpack config 放置预设以在Webpack配置中使用

Also, I have pushed my code to my GitHub repo ( https://github.com/Gum-Joe/bedel ). 另外,我已将代码推送到我的GitHub存储库( https://github.com/Gum-Joe/bedel )。 Feel free to have a look at it. 随意看看。

You configured the loader to only pass .jsx files through Babel: 您将加载程序配置为仅通过Babel传递.jsx文件:

test: /\.jsx$/

However, your file has the extension .js . 但是,您的文件扩展名为.js Either rename the file or update the test expression to /\\.jsx?$/ . 重命名文件或将测试表达式更新为/\\.jsx?$/

In addition to updating the test , you need to rename .babel.rc to .babelrc (no . before rc ). 除了更新test ,您还需要将.babel.rc重命名为.babelrc (在rc之前没有. )。 Otherwise Babel thinks that there is no configuration file and won't load any presets. 否则,Babel会认为没有配置文件,并且不会加载任何预设。

The loaders property must exist within the module property. 加载程序属性必须存在于模块属性内。 Webpack Loaders Webpack加载器

module.exports = {
  // ...

  // Output to /build
  output: {
    path: path.join(__dirname, "build", "js"),
    filename: "bundle.js"
  },

  module: {
    loaders: [
      { test: /\.jsx$/, exclude: /node_modules/, loader: "babel-loader" }
    ]
  },

  //...
};

You need to use react-preset with babel, like here: 您需要对babel使用react-preset,例如:

loaders: [{
  test: /\.(js|jsx)$/,
  loader: 'babel',
  query: {
    presets: [
      'es2015',
      'react'
    ]
  }
}]

I'm having this issue as well, and if you're using Windows and Node 6.x, the only workaround I've found for now seems to be to use Node 4 LTS or 5 instead. 我也遇到了这个问题,如果您使用的是Windows和Node 6.x,我目前发现的唯一解决方法似乎是改用Node 4 LTS或5。 Without knowing the root cause, the problem seems to stem from some combination of using JSX, Webpack, Babel, Acorn JS, Node 6, and Windows. 不知道根本原因,问题似乎源于使用JSX,Webpack,Babel,Acorn JS,Node 6和Windows的某种组合。

https://github.com/coryhouse/pluralsight-redux-starter/issues/2 https://github.com/danmartinez101/babel-preset-react-hmre/issues/32 https://github.com/coryhouse/pluralsight-redux-starter/issues/2 https://github.com/danmartinez101/babel-preset-react-hmre/issues/32

Can you try wrapping the entire 你可以尝试包装整个

element in brackets "()"? 括号“()”中的元素?

return (<p>...</p>)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM