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

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

问题描述

我是 React + Webpack 的初学者.

I'm a beginner in React + Webpack.

我在 hello world 网络应用中发现了一个奇怪的错误.

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

如果你使用 react 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 语法错误:意外的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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