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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52092739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:56:30  来源:igfitidea点击:

Upgrade to Babel 7: Cannot read property 'bindings' of null

javascriptnode.jsbabeljs

提问by rap-2-h

I just upgraded to Babel 7(from 6) by running these commands:

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

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

Here is my .babelrcfile:

这是我的.babelrc文件:

{ "presets": ["env"] }

Then I ran:

然后我跑了:

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

And it results in:

结果是:

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)

What did I do wrong?

我做错了什么?

回答by connexo

In your .babelrcfile, change

在您的.babelrc文件中,更改

{ "presets": ["env"] } 

to

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

(and install that package if you haven't already).

(如果您还没有安装该软件包,请安装该软件包)。

In your .babelrcyou are still referencing the package babel-preset-env(which is for 6.x), you want to reference @babel/preset-envinstead (which is for 7.x).

在您.babelrc仍然引用包babel-preset-env(适用于 6.x)中,您想要引用@babel/preset-env(适用于 7.x)。

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

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

Note:you should also make this change in webpack.config.jsif it is there as well.

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

Here is the sample webpack config section where you should change the preset:

这是您应该更改预设的示例 webpack 配置部分:

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