我的巴贝尔怎么了?
What's wrong with my babel?
我用 npm install -g babel-cli
安装了 babel 并用 babel-node --version
测试了它。输出是 6.2.0.
然后我测试如下。
C:\Users\xuhang\Documents\work\CDT\cdt3.0\babel>babel-node
> console.log([1,2,3].map(x => x * x))
repl:1
console.log([1, 2, 3].map(x => x * x));
^^
SyntaxError: Unexpected token =>
at Object.exports.runInThisContext (vm.js:73:16)
at _eval (C:\Users\xuhang\AppData\Roaming\npm\node_modules\babel-cli\lib\_b
abel-node.js:102:26)
at REPLServer.replEval (C:\Users\xuhang\AppData\Roaming\npm\node_modules\ba
bel-cli\lib\_babel-node.js:187:14)
at bound (domain.js:254:14)
at REPLServer.runBound [as eval] (domain.js:267:12)
at REPLServer.<anonymous> (repl.js:279:12)
at REPLServer.emit (events.js:107:17)
at REPLServer.Interface._onLine (readline.js:214:10)
at REPLServer.Interface._line (readline.js:553:8)
at REPLServer.Interface._ttyWrite (readline.js:830:14)
>
你能告诉我 console.log([1,2,3].map(x => x * x))
有什么问题吗?
另一个类似的问题如下。学babel太难了,初试都失败了
目录source里的是下面React官方提供的示例里的example.js
https://facebook.github.io/react/docs/tutorial.html
C:\Users\xuhang\Documents\work\CDT\cdt3.0\babel>babel source/ --watch --out-dir
build/
SyntaxError: source/example.js: Unexpected token (17:6)
15 | var rawMarkup = marked(this.props.children.toString(), {sanitize: tru
e});
16 | return (
> 17 | <div className="comment">
| ^
18 | <h2 className="commentAuthor">
19 | {this.props.author}
20 | </h2>
您忘记创建 .babelrc
文件。 6.x 之前的 Babel 版本包含一些用于 Babel 的预设,可以开箱即用地转换代码,在 6.x 中它没有,所以你需要安装你想要的预设,在你的情况下你需要两个:babel-preset-es2015
和 babel-preset-react
.
安装完它们后 npm install babel-preset-es2015 babel-preset-react
将它们添加到您的 .babelrc
文件
{
"presets": ["es2015", "react"]
}
我用 npm install -g babel-cli
安装了 babel 并用 babel-node --version
测试了它。输出是 6.2.0.
然后我测试如下。
C:\Users\xuhang\Documents\work\CDT\cdt3.0\babel>babel-node
> console.log([1,2,3].map(x => x * x))
repl:1
console.log([1, 2, 3].map(x => x * x));
^^
SyntaxError: Unexpected token =>
at Object.exports.runInThisContext (vm.js:73:16)
at _eval (C:\Users\xuhang\AppData\Roaming\npm\node_modules\babel-cli\lib\_b
abel-node.js:102:26)
at REPLServer.replEval (C:\Users\xuhang\AppData\Roaming\npm\node_modules\ba
bel-cli\lib\_babel-node.js:187:14)
at bound (domain.js:254:14)
at REPLServer.runBound [as eval] (domain.js:267:12)
at REPLServer.<anonymous> (repl.js:279:12)
at REPLServer.emit (events.js:107:17)
at REPLServer.Interface._onLine (readline.js:214:10)
at REPLServer.Interface._line (readline.js:553:8)
at REPLServer.Interface._ttyWrite (readline.js:830:14)
>
你能告诉我 console.log([1,2,3].map(x => x * x))
有什么问题吗?
另一个类似的问题如下。学babel太难了,初试都失败了
目录source里的是下面React官方提供的示例里的example.js
https://facebook.github.io/react/docs/tutorial.html
C:\Users\xuhang\Documents\work\CDT\cdt3.0\babel>babel source/ --watch --out-dir
build/
SyntaxError: source/example.js: Unexpected token (17:6)
15 | var rawMarkup = marked(this.props.children.toString(), {sanitize: tru
e});
16 | return (
> 17 | <div className="comment">
| ^
18 | <h2 className="commentAuthor">
19 | {this.props.author}
20 | </h2>
您忘记创建 .babelrc
文件。 6.x 之前的 Babel 版本包含一些用于 Babel 的预设,可以开箱即用地转换代码,在 6.x 中它没有,所以你需要安装你想要的预设,在你的情况下你需要两个:babel-preset-es2015
和 babel-preset-react
.
安装完它们后 npm install babel-preset-es2015 babel-preset-react
将它们添加到您的 .babelrc
文件
{
"presets": ["es2015", "react"]
}