运行React包含jsx标签的项目时发现报错:
Compiled with problems:X
ERROR in ./src/index.jsx
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /Users/maomao/Documents/demo/pc/askteam/src/index.jsx: Support for the experimental syntax 'jsx' isn't currently enabled (36:3):
34 |
35 | ReactDOM.render(
> 36 | <ConfigProvider locale={zhCN}>
| ^
37 | <App />
38 | </ConfigProvider>,
39 | document.getElementById('root')
Add @babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to enable parsing.
at Parser._raise (/Users/maomao/Documents/demo/pc/askteam/node_modules/@babel/parser/lib/index.js:510:17)
at Parser.raiseWithData (/Users/maomao/Documents/demo/pc/askteam/node_modules/@babel/parser/lib/index.js:503:17)
at Parser.expectOnePlugin (/Users/maomao/Documents/demo/pc/askteam/node_modules/@babel/parser/lib/index.js:3383:18)
at Parser.parseExprAtom (/Users/maomao/Documents/demo/pc/askteam/node_modules/@babel/parser/lib/index.js:11642:20)
at Parser.parseExprSubscripts (/Users/maomao/Documents/demo/pc/askteam/node_modules/@babel/parser/lib/index.js:11217:23)
at Parser.parseUpdate (/Users/maomao/Documents/demo/pc/askteam/node_modules/@babel/parser/lib/index.js:11197:21)
at Parser.parseMaybeUnary (/Users/maomao/Documents/demo/pc/askteam/node_modules/@babel/parser/lib/index.js:11172:23)
at Parser.parseMaybeUnaryOrPrivate (/Users/maomao/Documents/demo/pc/askteam/node_modules/@babel/parser/lib/index.js:10986:59)
at Parser.parseExprOps (/Users/maomao/Documents/demo/pc/askteam/node_modules/@babel/parser/lib/index.js:10993:23)
at Parser.parseMaybeConditional (/Users/maomao/Documents/demo/pc/askteam/node_modules/@babel/parser/lib/index.js:10963:23)
报错原因
缺少插件:@babel/preset-react
解决方案
安装@babel/preset-react
插件并添加在babel配置文件里。
1. 安装:
npm install @babel/preset-react --save-dev
2. 配置:在.babelrc
文件的presets
属性中新增"@babel/preset-react"
.babelrc文件
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-transform-runtime",
"@babel/plugin-proposal-class-properties"
]
}
3. 重新运行项目
暂无评论