升级到Babel 7:无法读取null的属性“绑定”


118

通过运行以下命令,我刚刚从6 升级到Babel 7

npm remove babel-cli
npm install --save-dev @babel/cli @babel/core @babel/preset-env

这是我的.babelrc文件:

{ "presets": ["env"] }

然后我跑了:

babel js/src --out-dir js/dist

结果是:

TypeError: Cannot read property 'bindings' of null
    at Scope.moveBindingTo (/xyz/node_modules/@babel/traverse/lib/scope/index.js:867:13)
    at BlockScoping.updateScopeInfo (/xyz/node_modules/babel-plugin-transform-es2015-block-scoping/lib/index.js:364:17)
    at BlockScoping.run (/xyz/node_modules/babel-plugin-transform-es2015-block-scoping/lib/index.js:330:12)
    at PluginPass.BlockStatementSwitchStatementProgram (/xyz/node_modules/babel-plugin-transform-es2015-block-scoping/lib/index.js:70:24)
    at newFn (/xyz/node_modules/@babel/traverse/lib/visitors.js:193:21)
    at NodePath._call (/xyz/node_modules/@babel/traverse/lib/path/context.js:53:20)
    at NodePath.call (/xyz/node_modules/@babel/traverse/lib/path/context.js:40:17)
    at NodePath.visit (/xyz/node_modules/@babel/traverse/lib/path/context.js:88:12)
    at TraversalContext.visitQueue (/xyz/node_modules/@babel/traverse/lib/context.js:118:16)
    at TraversalContext.visitSingle (/xyz/node_modules/@babel/traverse/lib/context.js:90:19)

我做错什么了?

Answers:


306

在您的.babelrc文件中,更改

{ "presets": ["env"] } 

{ "presets": ["@babel/preset-env"] }

(如果尚未安装,请安装该软件包)。

在您.babelrc仍在引用程序包babel-preset-env(适用于6.x)的情况下,您想要引用@babel/preset-env(对于7.x适用)。

https://github.com/babel/babel/issues/6186#issuecomment-366556833

注意:webpack.config.js如果还存在,您也应该进行此更改。

这是样本Webpack配置部分,您应在其中更改预设:

use: {
  loader: 'babel-loader',
  options: {
    // Here you should change 'env' to '@babel/preset-env'
    presets: ['@babel/preset-env'],
  },
},

8
请注意,webpack.config.js如果还存在此更改,也应进行更改。
鹰眼
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.