react 中bable转换 jsx为 js 出错
运行
npm run dev
报错
将jsx转化为js代码报错,求解
[89] ./app/main.js 638 bytes {0} [built] [failed] [1 error]
+ 75 hidden modules
ERROR in ./app/main.js
Module build failed: SyntaxError: Unexpected token (16:17)
14 |
15 | function main() {
> 16 | React.render(<Hello/>, document.getElementById('app'));
| ^
17 | }
配置文件
var path = require('path');
var config = {
entry: [
'webpack/hot/dev-server',
'webpack-dev-server/client?http://localhost:8080',
path.resolve(__dirname, 'app/main.js')
],
output: {
path: path.resolve(__dirname, 'build'),
filename: 'bundle.js'
},
module: {
loaders: [{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/
}]
}
};
module.exports = config;
package.json文件
{
"name": "application-name",
"version": "0.0.1",
"dependencies": {
"react": "^15.6.1",
"webpack": "^3.4.1",
"webpack-dev-server": "^2.6.1"
},
"scripts": {
"build": "webpack",
"dev": "webpack-dev-server"
},
"devDependencies": {
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1"
}
}
component.jsx文件
import React from 'react';
export default class Hello extends React.Component {
render() {
return <h1>Hello!!!</h1>;
}
}
main.js
import React from 'react';
import Hello from './component.jsx';
main();
function main() {
React.render(<Hello/>, document.getElementById('app'));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
需要安装
babel-preset-react
、babel-preset-es2015
然后配置一下你的
.babelrc
,如果没有就新增