【问题标题】:Node.js and ES6 [duplicate]Node.js 和 ES6 [重复]
【发布时间】:2016-11-29 15:12:53
【问题描述】:

我想开始在 Node.js 上使用这个 ES6,但我不能让它工作。我正在添加 node test.js --harmony 标志,但我仍然遇到语法错误。

它甚至没有通过第一行代码:

import env from 'node-env-file'
import api from '../src'

我收到了

(function (exports, require, module, __filename, __dirname) { import env from 'node-env-file'
                                                              ^^^^^^
SyntaxError: Unexpected token import

如何让我的 Node.js 使用这种类型的语法。

【问题讨论】:

  • 目前,本机不支持模块。您可以在node.green 找到哪些版本包含哪些功能的摘要。同时,您可以使用Babel等工具进行翻译。

标签: javascript node.js coffeescript ecmascript-6 es6-promise


【解决方案1】:

import 关键字尚不被任何版本的 node 支持,因为还没有原生支持 ES6 模块的 JavaScript 引擎。

你必须使用某种 ES6 到 ES5 的转译器,比如 Babel,或者坚持使用 require

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 2018-08-02
    • 2018-07-18
    相关资源
    最近更新 更多