我正在尝试构建我的 React 库,但 npm build 给出了这个错误。是什么导致了这个错误以及如何解决它?
src/lib/CircularProfiles.js -> dist/CircularProfiles.js
SyntaxError: src/lib/Github.js: Unexpected token (14:10)
12 | class GithubProfileBar extends Component {
13 |
> 14 | state = {
| ^
15 | totalRepos: 0,
16 | totalStars: 0,
17 | }
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! react-profiles@0.1.0 build: `rm -rf dist && NODE_ENV=production babel src/lib --out-dir dist --copy-files --ignore __tests__,spec.js,test.js,__snapshots__`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the react-profiles@0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/natesh/.npm/_logs/2018-12-26T03_51_21_931Z-debug.log
我的 .babelrc 文件:
{
"presets": [
"es2015",
"react"
]
}
最佳答案
我发现错误是因为旧版本的 babel 无法处理新版本的 React 代码。
修复方法如下:
我的问题是旧的 babel 版本很容易通过安装修复:
npm i -D @babel/plugin-proposal-class-properties \
@babel/preset-react \
@babel/preset-env \
@babel/core \
@babel/plugin-transform-runtime \
在 .babelrc 文件中:
{
"presets": [
"@babel/react" ,
"@babel/env" ,
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
现在 babel 构建成功了。
关于javascript - Babel 在构建时给出意外的 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53927260/