繁体   English   中英

我的通天塔怎么了?

[英]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-es2015babel-preset-es2015babel-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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM