本文翻译自:babel-loader jsx SyntaxError: Unexpected token [duplicate]

This question already has an answer here: 这个问题已经在这里有了答案:

  • Babel file is copied without being transformed 9 answers 通天塔文件被复制而不进行转换 9答案

I'm a beginner in React + Webpack. 我是React + Webpack的初学者。

I found a weird error in my hello world web app. 我在Hello World Web应用程序中发现了一个奇怪的错误。

I'm using babel-loader in webpack to help me convert jsx into js, but it seems like babel can't understand jsx syntax. 我在webpack中使用babel-loader来帮助我将jsx转换为js,但是babel似乎无法理解jsx语法。

Here are my dependencies: 这是我的依赖项:

"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"
}

Here is my webpack.config.js 这是我的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"}]}
};

Here is my app/main.js 这是我的app/main.js

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

And this is the error message 这是错误消息

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)

Thanks for you guys. 谢谢你们


#1楼

参考:https://stackoom.com/question/2GOaC/babel-loader-jsx-SyntaxError-意外令牌-重复


#2楼

Add "babel-preset-react" 添加“ babel-preset-react”

npm install babel-preset-react

and add "presets" option to babel-loader in your webpack.config.js 并在webpack.config.js中向babel-loader添加“预设”选项

(or you can add it to your .babelrc or package.js: http://babeljs.io/docs/usage/babelrc/ ) (或者您可以将其添加到您的.babelrc或package.js: http ://babeljs.io/docs/usage/babelrc/)

Here is an example webpack.config.js: 这是一个webpack.config.js示例:

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

Recently Babel 6 was released and there was a major change: https://babeljs.io/blog/2015/10/29/6.0.0 最近发布了Babel 6,并进行了重大更改: https : //babeljs.io/blog/2015/10/29/6.0.0

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 如果您使用ReactDOM.render() 0.14,则应该使用ReactDOM.render() (来自require('react-dom') )而不是React.render() : https : React.render()

UPDATE 2018 更新2018

Rule.query has already been deprecated in favour of Rule.options. 不推荐使用Rule.query,而使用Rule.options。 Usage in webpack 4 is as follows: webpack 4中的用法如下:

npm install babel-loader babel-preset-react

Then in your webpack configuration (as an entry in the module.rules array in the module.exports object) 然后在您的webpack配置中(作为module.exports对象中module.rules数组中的条目)

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

#3楼

Since the answer above still leaves some people in the dark, here's what a complete webpack.config.js might look like: 由于上面的答案仍然使某些人望而却步,因此完整的webpack.config.js可能如下所示:

 var path = require('path'); var config = { entry: path.resolve(__dirname, 'app/main.js'), output: { path: path.resolve(__dirname, 'build'), filename: 'bundle.js' }, module: { loaders: [{ test: /\\.jsx?$/, loader: 'babel', query: { presets:['es2015', 'react'] } }] }, }; module.exports = config; 

#4楼

I ran into a similar issue when migrating from babel 5 to babel 6. 从babel 5迁移到babel 6时,我遇到了类似的问题。

I was just running babel to compile the src to lib folder babel src --out-dir lib 我只是在运行babel将src编译为lib文件夹babel src --out-dir lib

I will share my setup for babel 6: 我将分享Babel 6的设置:

Ensure you have the following babel 6 devDependencies installed 确保您已安装以下babel 6 devDependencies

"babel-core": "^6.7.6",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0"

Add your .babelrc file to the project: .babelrc文件添加到项目中:

{"presets": ["es2015", "stage-0", "react"]
}

#5楼

The following way has helped me (includes react-hot, babel loaders and es2015, react presets): 以下方法对我有所帮助(包括react-hot,babel装载程序和es2015,react预设):

loaders: [{test: /\.jsx?$/,exclude: /node_modules/,loaders: ['react-hot', 'babel?presets[]=es2015&presets[]=react']}
]

#6楼

You can find a really good boilerplate made by Henrik Joreteg (ampersandjs) here: https://github.com/HenrikJoreteg/hjs-webpack 您可以在这里找到由Henrik Joreteg(&sandjs)制作的非常好的样板: https : //github.com/HenrikJoreteg/hjs-webpack

Then in your webpack.config.js 然后在您的webpack.config.js

var getConfig = require('hjs-webpack')module.exports = getConfig({in: 'src/index.js',out: 'public',clearBeforeBuild: true,https: process.argv.indexOf('--https') !== -1
})

babel-loader jsx SyntaxError:意外令牌[重复]相关推荐

  1. vscode运行vue时,显示loader. js SyntaxError: Unexpected identifier

    错误信息: Failed to compile. Error: Child compilation failed: Module build failed(from./node_modules/_ht ...

  2. 我不断收到“ Uncaught SyntaxError:意外令牌o”

    本文翻译自:I keep getting "Uncaught SyntaxError: Unexpected token o" I'm trying to learn some h ...

  3. java 判断文章的重复率_5个避免意外论文重复率高的方法

    即使你不是故意抄袭,但你可能在无意中抄袭了别人的论文, 这个叫做意外抄袭,它可能正发生在你身上,如果你不熟悉学术 道德规范,这里将告诉你5个基本的方法来避免意外抄袭. Tip1 熟悉其他人的论文 请检 ...

  4. build怎么调试 react_webpack打包调试react并使用babel编译jsx配置方法

    http://lxj8749.iteye.com/blog/2287074 ********************************************** 安装webpack npm i ...

  5. 错题集一,基础错题集

    未捕获的SyntaxError:意外令牌') 解决:检查是否缺少/多括号 2. 问题:非法返回值 解决:不需要返回return值

  6. 用babel cli编译用ES6写的JSX

    安装babel及其插件 npm install babel babel-cli babel-preset-es2015 babel-preset-react 把ES6转为ES5 babel demo. ...

  7. react编译器jsxTransformer,babel

    1.JSX是什么 JSX其实是JavaScript的扩展,React为了代码的可读性更方便地创建虚拟DOM等原因,加入了一些类似XML的语法的扩展. 2.编译器--jsxTransformer JSX ...

  8. webpack loader配置全流程详解

    前言 1.主要目的为稍微梳理从配置到装载的流程.另外详解当然要加点源码提升格调(本人菜鸟,有错还请友善指正) 2.被的WebPack打包的文件,都被转化为一个模块,比如import './xxx/x. ...

  9. Babel:plugin、preset的区别与使用

    文章概览 本文主要内容包括:什么是Babel Plugin.Babel Preset,两者的区别与联系.如何使用plugin/preset.如何创建自定义preset.使用注意事项. 本文所有例子可在 ...

最新文章

  1. mysql client pip_mac pip install mysqlclient 报错
  2. window的onresize执行多次的解决方法
  3. java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal res
  4. cubemx stm32 配置两个串口_STM32CubeMX的串口配置,以及驱动代码
  5. 【Oracle】Rman备份策略
  6. apt 和 apt-get 区别
  7. 百度App网络深度优化系列《一》DNS优化
  8. javase转javaee(java web)
  9. Excel 透视表 - 人生第二个透视表
  10. anaconda怎么切换目录_Anaconda更改工作路径
  11. 计算机开机错误怎么办,电脑开机蓝屏错误代码0X0000007E怎么办?
  12. 计算与推断思维 十六、比较两个样本
  13. 三国志战略版:【满红杀手】名将卡包新武将_许攸分析
  14. 阿里云ECS静态建站学习
  15. 小米打印机显示服务器出错,关于小米笔记本连接不上网络打印机的处理方法
  16. 如何系统地学习计算机编程?自学还是培训?怎么选?
  17. MIXLAB_NASA_TICKET生成
  18. 山洪灾害监测预警系统解决方案
  19. 关于错误Failure [INSTALL_FAILED_ALREADY_EXISTS]的解决办法
  20. 华为,好狠一公司,应届生你给201万

热门文章

  1. 使用CXF 来发布一个 service
  2. 王立平--android中的anim(动画)
  3. iOS的view翻转动画实现--代码老,供参考
  4. Oracle 声明常量
  5. ASP.NET 2.0构建动态导航的Web应用程序(TreeView和Menu )[转载]
  6. Silverlight AutoCompleteBox(自动完成输入框控件)使用方法
  7. 软件开发工程师:程序员的职业规划就像软件工程
  8. kubernetes发布tomcat服务,通过deployment,service布署
  9. 输入一个日期,计算这个日期与 2018-03-12差多少天;
  10. bzoj1588 营业额统计