Javascript Babel 6 CLI:意外的令牌导出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33448675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Babel 6 CLI: Unexpected token export?
提问by iwatakeshi
I'm trying to run Babel through it's CLI using babel-node
but I keep getting the Unexpected token export
error. 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.
我正在尝试通过它的 CLI 运行 Babel,babel-node
但我不断收到Unexpected token export
错误消息。我知道 Babel 6 是关于插件的,我需要设置插件,.babelrc
但它似乎无法正常工作。
So here are my questions:
所以这里是我的问题:
- Should I be using the syntax-export-extensions plugin? I've also tried using the alternative method which is setting the plugin through
package.json
but still no luck. - Also, does Babel 6's CLI have a global
.babelrc
option? It seems tedious if I have to install the plugins for every project that requires it...
- 我应该使用syntax-export-extensions 插件吗?我也试过使用另一种方法来设置插件,
package.json
但仍然没有运气。 - 另外,Babel 6 的 CLI 有全局
.babelrc
选项吗?如果我必须为每个需要它的项目安装插件,这似乎很乏味......
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;
回答by James Kyle
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)
然后build
在你的 package.json 中添加一个运行 Babel的脚本:(这很重要,因为它将使用你的本地版本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? It seems tedious if I have to install the plugins for every project that requires it...
另外,Babel 6 的 CLI 是否有全局 .babelrc 选项?如果我必须为每个需要它的项目安装插件,这似乎很乏味......
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.
这是一个坏主意,因为这意味着如果不更新每个项目代码,您就无法更新它。拥有本地版本意味着这种潜在错误不太可能发生。
回答by user3405291
I received the same error, but my webpack/babel configs looked correct. By trial and error, I replaced export myFunction
with export default myFunction
and the error got resolved.
我收到了同样的错误,但我的 webpack/babel 配置看起来是正确的。通过反复试验,我替换export myFunction
了export default myFunction
并且错误得到了解决。
Later, I realized that the correct way of exporting is export {myFunction}
. I implemented it and everything works fine.
后来才知道,正确的导出方式是export {myFunction}
. 我实施了它,一切正常。