Babel在构建时给Unexped令牌

Nat*_*hat 1 javascript babel reactjs webpack

我正在尝试构建我的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
Run Code Online (Sandbox Code Playgroud)

我的.babelrc文件:

{
    "presets": [
        "es2015",
        "react"
    ]
}
Run Code Online (Sandbox Code Playgroud)

Nat*_*hat 6

我发现错误是由于babel的较旧版本无法处理较新版本的react代码。

解决方法:

我的问题是可以通过安装轻松修复较旧的babel版本:

npm i @babel/plugin-proposal-class-properties @babel/preset-react @babel/preset-env @babel/core @babel/plugin-transform-runtime --save-dev
Run Code Online (Sandbox Code Playgroud)

在.babelrc文件中:

{
   "presets": [
       "@babel/react" , 
       "@babel/env" , 
   ],
   "plugins": [
       "@babel/plugin-proposal-class-properties"
   ]
}
Run Code Online (Sandbox Code Playgroud)

现在,babel在此之后成功构建了它。