【问题标题】:What's wrong with my babel?我的巴贝尔怎么了?
【发布时间】:2016-02-29 04:10:51
【问题描述】:

我已经用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 =&gt; 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>

【问题讨论】:

    标签: node.js reactjs webpack babeljs


    【解决方案1】:

    您忘记创建.babelrc 文件。 6.x 之前的 Babel 版本包含一些预设,用于 Babel 转译代码开箱即用,在 6.x 中它没有,所以你需要安装你想要的预设,在你的情况下你需要两个:babel-preset-es2015 和 @ 987654323@.

    将它们都安装好后,npm install babel-preset-es2015 babel-preset-react 将它们添加到您的 .babelrc 文件中

    {
        "presets": ["es2015", "react"]
    }
    

    【讨论】:

    • 它是 babel-preset-es2015(正如您在示例 .babelrc 中正确使用的那样)
    猜你喜欢
    • 2016-02-11
    • 1970-01-01
    • 2012-09-05
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多