升级babel7踩坑记

项目中升级Babel 7后,运行错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Unknown substitution "BODY" given

at Object.keys.forEach.key (node_modules/@babel/template/lib/populate.js:35:15)
at Array.forEach (<anonymous>)
at populatePlaceholders (node_modules/@babel/template/lib/populate.js:33:31)
at arg (node_modules/@babel/template/lib/string.js:22:51)
at arg (node_modules/@babel/template/lib/builder.js:77:14)
at spec (node_modules/babel-plugin-transform-es2015-for-of/lib/index.js:171:20)
at PluginPass.ForOfStatement (node_modules/babel-plugin-transform-es2015-for-of/lib/index.js:76:21)
at newFn (node_modules/@babel/traverse/lib/visitors.js:193:21)
at NodePath._call (node_modules/@babel/traverse/lib/path/context.js:53:20)
at NodePath.call (node_modules/@babel/traverse/lib/path/context.js:40:17)
=============
at exports.default (node_modules/babel-plugin-transform-es2015-for-of/lib/index.js:14:20)
at loadDescriptor (node_modules/@babel/core/lib/config/full.js:165:14)
at cachedFunction (node_modules/@babel/core/lib/config/caching.js:33:19)
at loadPluginDescriptor (node_modules/@babel/core/lib/config/full.js:200:28)
at config.plugins.reduce (node_modules/@babel/core/lib/config/full.js:69:20)
at Array.reduce (<anonymous>)
at recurseDescriptors (node_modules/@babel/core/lib/config/full.js:67:38)
at recurseDescriptors (node_modules/@babel/core/lib/config/full.js:94:27)



TypeError: Cannot read property 'bindings' of null

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

.babelrc 配置:

1
{ "presets": ["env"] }

解决方案:
.babelrc改为

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

参考链接
Upgrade to Babel 7: Cannot read property ‘bindings’ of null