[英]npm/gulp-babel: target works everywhere BUT on my machine, what's wrong?
[英]What's wrong with my babel?
我已经使用npm install -g babel-cli
并使用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))
怎么了吗?
下面是另一个类似的问题。 我很难学通天塔,初始测试都失败了。
目录源内部是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
文件。 Babel 6.x之前的版本包含一些预设,供Babel即时使用,可以在代码中移植代码,而在6.x中则不需要,因此您需要安装所需的预设,在这种情况下,您需要两个babel-preset-es2015
: babel-preset-es2015
和babel-preset-react
。
将它们都npm install babel-preset-es2015 babel-preset-react
后,两个npm install babel-preset-es2015 babel-preset-react
将它们添加到您的.babelrc
文件中
{
"presets": ["es2015", "react"]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.