简体   繁体   English

Babel 6 CLI:意外的令牌导出?

[英]Babel 6 CLI: Unexpected token export?

I'm trying to run Babel through it's CLI using babel-node but I keep getting the Unexpected token export error. 我正在尝试使用babel-node通过它的CLI运行Babel,但我不断收到Unexpected token export错误。 I understand that Babel 6 is all about plugins and that I need to set the plugin through .babelrc but it doesn't seem to work properly. 我知道Babel 6是关于插件的,我需要通过.babelrc设置插件,但它似乎无法正常工作。

So here are my questions: 所以这是我的问题:

For those who are curious of what I'm trying to export, then here is the class: 对于那些对我想要输出的东西感到好奇的人来说,这里是班级:

'use strict';

class Factorial {
  static solve (num) {
    if(num === 0) return 1;
    else return num * Factorial.solve(num - 1);
  }
}

console.log(Factorial.solve(5))

export default Factorial;

The easiest way to get started is to use a preset . 最简单的入门方法是使用预设

First let's install our dependencies: 首先让我们安装我们的依赖项:

$ npm install --save-dev babel-cli babel-preset-es2015

Then add a build script to your package.json that runs Babel: (this is important because it will use your local version of babel-cli instead of a globally installed one) 然后在运行Babel的package.json中添加一个build脚本:(这很重要,因为它将使用本地版本的babel-cli而不是全局安装的)

"build": "babel input.js"

Your package.json should look like this: 你的package.json应该是这样的:

{
  "name": "my-module",
  "devDependencies": {
    "babel-cli": "^6.x.x",
    "babel-preset-es2015": "^6.x.x"
  },
  "scripts": {
    "build": "babel input.js -o compiled.js"
  }
}

Finally you want to update your local .babelrc like this: 最后你要像这样更新你的本地.babelrc

{
  "presets": ["es2015"]
}

Then you run npm run build and you're all set to go. 然后你运行npm run build ,你就可以了。

Also, does Babel 6's CLI have a global .babelrc option? 另外,Babel 6的CLI有全局.babelrc选项吗? It seems tedious if I have to install the plugins for every project that requires it... 如果我必须为每个需要它的项目安装插件,这似乎很乏味......

That's a bad idea as it means you can't ever update it without updating every single one of your projects code. 这是一个坏主意,因为这意味着如果不更新每一个项目代码,就无法更新它。 Having local versions means this potential error is less likely to occur. 拥有本地版本意味着不太可能发生这种潜在错误。

I received the same error, but my webpack/babel configs looked correct. 我收到了同样的错误,但我的webpack / babel配置看起来是正确的。 By trial and error, I replaced export myFunction with export default myFunction and the error got resolved. 通过反复试验,我用export default myFunction替换了export myFunction ,错误得到了解决。


Later, I realized that the correct way of exporting is export {myFunction} . 后来,我意识到导出的正确方法是export {myFunction} I implemented it and everything works fine. 我实现了它,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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