babel-loader jsx SyntaxError:意外的令牌 [英] babel-loader jsx SyntaxError: Unexpected token

查看:275
本文介绍了babel-loader jsx SyntaxError:意外的令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是React + Webpack的初学者。

I'm a beginner in React + Webpack.

我在我的hello world web app中发现了一个奇怪的错误。

I found a weird error in my hello world web app.

我在webpack中使用babel-loader来帮助我将jsx转换为js,但看起来babel无法理解jsx语法。

I'm using babel-loader in webpack to help me convert jsx into js, but it seems like babel can't understand jsx syntax.

这里是我的依赖项:

"devDependencies": {
  "babel-core": "^6.0.14",
  "babel-loader": "^6.0.0",
  "webpack": "^1.12.2",
  "webpack-dev-server": "^1.12.1"
},
"dependencies": {
  "react": "^0.14.1"
}

这是我的 webpack.config.js

var path = require('path');
module.exports = {
  entry: ['webpack/hot/dev-server',path.resolve(__dirname, 'app/main.js')],
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'bundle.js'
  },
  module: {
      loaders: [
          { test: /\.js$/, exclude: /node_modules/, loader: "babel-loader"}
      ]
  }
};

这是我的 app / main.js

var React = require("react");
React.render(<h1>hello world</h1>,document.getElementById("app"));

这是错误信息

ERROR in ./app/main.js
Module build failed: SyntaxError: ~/**/app/main.js: Unexpected token (2:13)
  1 | var React = require("react");
> 2 | React.render(<h1>hello world</h1>,document.getElementById("app"));
    |              ^
at Parser.pp.raise (~/**/node_modules/babylon/lib/parser/location.js:24:13)

谢谢你们。

推荐答案

添加babel-preset-react

Add "babel-preset-react"

npm install babel-preset-react

并在webpack.config.js中为babel-loader添加预设选项

and add "presets" option to babel-loader in your webpack.config.js

(或者你可以将它添加到你的.babelrc或package.js: http://babeljs.io/docs/usage/babelrc/

(or you can add it to your .babelrc or package.js: http://babeljs.io/docs/usage/babelrc/)

以下是webpack.config.js的示例:

Here is an example webpack.config.js:

{ 
    test: /\.jsx?$/,         // Match both .js and .jsx files
    exclude: /node_modules/, 
    loader: "babel", 
    query:
      {
        presets:['react']
      }
}

最近发布了Babel 6并发生了重大变化:
https://babeljs.io/blog/2015/10/29/6.0.0

Recently Babel 6 was released and there was a major change: https://babeljs.io/blog/2015/10/29/6.0.0

如果您正在使用反应0.14,你应该使用 ReactDOM.render()(来自 require('react-dom'))而不是 React.render() https:/ /facebook.github.io/react/blog/#changelog

If you are using react 0.14, you should use ReactDOM.render() (from require('react-dom')) instead of React.render(): https://facebook.github.io/react/blog/#changelog

更新2018年

Rule.query已被弃用,转而使用Rule.options。 webpack 4中的用法如下:

Rule.query has already been deprecated in favour of Rule.options. Usage in webpack 4 is as follows:

npm install babel-loader babel-preset-react

然后在你的webpack配置中(作为module.exports对象中module.rules数组中的一个条目)

Then in your webpack configuration (as an entry in the module.rules array in the module.exports object)

{
    test: /\.jsx?$/,
    exclude: /node_modules/,
    use: [
      {
        loader: 'babel-loader',
        options: {
          presets: ['react']
        }
      }
    ],
  }

这篇关于babel-loader jsx SyntaxError:意外的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆