本文介绍了“意外的令牌导入"在 Nodejs5 和 babel 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 js 文件中,我使用 import to 而不是 require

In js file, i used import to instead of require

import co from 'co';

并尝试通过 nodejs 直接运行它,因为它说导入是运输功能"并且支持没有任何运行时标志 (https://nodejs.org/en/docs/es6/),但出现错误

And tried to run it directly by nodejs since it said import is 'shipping features' and support without any runtime flag (https://nodejs.org/en/docs/es6/), but i got an error

import co from 'co';
^^^^^^

SyntaxError: Unexpected token import

然后我尝试使用 babel

Then i tried to use 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

仍然出现同样的错误,意外的令牌导入?

still got same error, unexpected token import?

我怎样才能摆脱它?

推荐答案

来自 babel 6 发行说明:

From the babel 6 Release notes:

由于 Babel 专注于成为 JavaScript 工具平台而不是 ES2015 转译器,我们决定让所有插件都选择加入.这意味着当您安装 Babel 时,它将不再默认转译您的 ES2015 代码.

在我的设置中,我安装了 es2015 预设

In my setup I installed the es2015 preset

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

或者用纱线

yarn add babel-preset-es2015 --dev

并在我的 .babelrc 中启用预设

and enabled the preset in my .babelrc

{
  "presets": ["es2015"]
}

这篇关于“意外的令牌导入"在 Nodejs5 和 babel 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:22
查看更多