// runMain.js
'use strict';
require('babel-register');
require('./run.js');
// run.js
let o = {id: 1};
let o2 = {...o};
console.log(o === o2);
我这样做了:
npm install babel-preset-es2015
npm install babel-plugin-transform-object-rest-spread
和vi ~/.babelrc
{
"presets": ["es2015"],
"plugins": ["transform-object-rest-spread","syntax-object-rest-spread"]
}
但node runMain.js
C:\workspace\git\LEGO\activity-lego\node_modules\babel-register\node_modules\babel-core\lib\transformation\file\index.js:548
throw err;
^
SyntaxError: C:/workspace/git/LEGO/activity-lego/units/2015weather/run.js: Unexpected token (18:10)
[0m 16 | [90m// run.js[39m
17 | let o [1m=[22m [32m{[39mid[1m:[22m [35m1[39m[32m}[39m[1m;[22m
> 18 | let o2 [1m=[22m [32m{[39m[1m...[22mo[32m}[39m[1m;[22m
| ^
19 | console[1m.[22mlog[94m[1m([22m[39mo [1m===[22m o2[94m[1m)[22m[39m[1m;[22m[0m
at Parser.pp.raise (C:\workspace\git\LEGO\activity-lego\node_modules\babel-register\node_modules\babel-core\node_modules\babylon\lib\parser\locati
on.js:22:13)
at Parser.pp.unexpected (C:\workspace\git\LEGO\activity-lego\node_modules\babel-register\node_modules\babel-core\node_modules\babylon\lib\parser\u
til.js:91:8)
at Parser.pp.parseIdentifier (C:\workspace\git\LEGO\activity-lego\node_modules\babel-register\node_modules\babel-core\node_modules\babylon\lib\par
ser\expression.js:1009:10)
at Parser.pp.parsePropertyName (C:\workspace\git\LEGO\activity-lego\node_modules\babel-register\node_modules\babel-core\node_modules\babylon\lib\p
arser\expression.js:831:135)
at Parser.pp.parseObj (C:\workspace\git\LEGO\activity-lego\node_modules\babel-register\node_modules\babel-core\node_modules\babylon\lib\parser\exp
ression.js:747:12)
at Parser.pp.parseExprAtom (C:\workspace\git\LEGO\activity-lego\node_modules\babel-register\node_modules\babel-core\node_modules\babylon\lib\parse
r\expression.js:479:19)
at Parser.pp.parseExprSubscripts (C:\workspace\git\LEGO\activity-lego\node_modules\babel-register\node_modules\babel-core\node_modules\babylon\lib
\parser\expression.js:265:19)
at Parser.pp.parseMaybeUnary (C:\workspace\git\LEGO\activity-lego\node_modules\babel-register\node_modules\babel-core\node_modules\babylon\lib\par
ser\expression.js:245:19)
at Parser.pp.parseExprOps (C:\workspace\git\LEGO\activity-lego\node_modules\babel-register\node_modules\babel-core\node_modules\babylon\lib\parser
\expression.js:176:19)
at Parser.pp.parseMaybeConditional (C:\workspace\git\LEGO\activity-lego\node_modules\babel-register\node_modules\babel-core\node_modules\babylon\l
bebel版本6.2.0(babel-core 6.2.1) 节点版本4.0.0(64位) 操作系统版本:窗口7
答案 0 :(得分:2)
由于这个问题尚未得到解答,但谷歌的评价很高,而且我的反应应用程序没有使用babel-register (!)解决了同样的问题,我在这里发布了答案,也许这对其他人来说很有用:
解决方案1
基于https://github.com/babel/babel-loader/issues/170
首先安装:
npm install --save-dev babel-plugin-transform-es2015-destructuring
npm install --save-dev babel-plugin-transform-object-rest-spread
第二次编辑.babelrc:
{
"presets": ["react", "es2015"],
"plugins": ["transform-es2015-destructuring", "transform-object-rest-spread"]
}
解决方案2
基于http://stefan.magnuson.co/articles/frontend/using-es7-spread-operator-with-webpack/
首先安装:
npm install --save babel-preset-stage-2
第二次编辑.babelrc:
{
"presets" : ["es2015", "react", "stage-2"]
}
在提供的链接中,预设在webpack.config.js中设置,我不确定建议设置此设置的方法。
<强>结论强>
这两种方式让我使用... spread对象语法,错误“Unexpected token”消失了。
我还不确定推荐哪种方式,但我现在更喜欢第二种,因为它更容易处理。