Babel在构建时给Unexped令牌

时间:2018-12-26 03:56:17

标签: javascript reactjs webpack babel

我正在尝试构建我的React库,而npm build给出了此错误。是什么导致此错误以及如何解决?

@Entity
public class entityB{


@Id
private int id;

@JoinTable(name = "associated_entities",
        joinColumns = { @JoinColumn(name = "entityA_id") },
        inverseJoinColumns = { @JoinColumn(name = "entityB_id") })
private List<EntityA> entitiesA;

//getters and setters
}

我的.babelrc文件:

    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

2 个答案:

答案 0 :(得分:3)

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

解决方法:

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

npm i @babel/plugin-proposal-class-properties @babel/preset-react @babel/preset-env @babel/core @babel/plugin-transform-runtime --save-dev

在.babelrc文件中:

{
   "presets": [
       "@babel/react" , 
       "@babel/env" , 
   ],
   "plugins": [
       "@babel/plugin-proposal-class-properties"
   ]
}

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

答案 1 :(得分:0)

确保不要同时使用v7和v6分支和babel。 “ @ babel / core”是7x分支,“ babel / core”是6x分支,您不应该同时安装两者!