在 js 文件中,我使用 import to 而不是 require
import co from 'co';
并尝试通过 nodejs 直接运行它,因为它说导入是“运送功能”并且支持没有任何运行时标志 (https://nodejs.org/en/docs/es6/),但我遇到了错误
import co from 'co';
^^^^^^
SyntaxError: Unexpected token import
然后我尝试使用 babel
npm install -g babel-core
npm install -g babel-cli
npm install babel-core //install to babel locally, is it necessary?
并由
运行babel-node js.js
仍然有同样的错误,意外的 token 导入?
我怎样才能摆脱它?
最佳答案
来自 babel 6 发行说明:
Since Babel is focusing on being a platform for JavaScript tooling and not an ES2015 transpiler, we’ve decided to make all of the plugins opt-in. This means when you install Babel it will no longer transpile your ES2015 code by default.
在我的设置中,我安装了 es2015 预设
npm install --save-dev babel-preset-es2015
或者用 yarn
yarn add babel-preset-es2015 --dev
并在我的 .babelrc 中启用预设
{
"presets": ["es2015"]
}
关于javascript - Nodejs5 和 babel 中的 "unexpected token import"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33604470/