【问题标题】:ES2015 "import" not working in node v6.0.0 with with --harmony_modules optionES2015“导入”在带有 --harmony_modules 选项的节点 v6.0.0 中不起作用
【发布时间】:2016-08-22 10:04:18
【问题描述】:

我正在使用节点 v6.0.0 并想使用 ES2016 (ES6)。但是我意识到“导入”语法不起作用。 “导入”不是在 ES2015 中编写模块化代码的基础吗?我也尝试使用--harmony_modules 选项运行节点,但仍然遇到关于“导入”的相同错误。这是代码。

没有“导入”的工作代码:

'use strict';
let sum = 0;
class Number {

  addNumber(num1, num2) {
    return num1 + num2;
  }
}
let numberObj = new Number();
sum = numberObj.addNumber(1,2);
console.log("sum of two number 1 and 2 "+ sum);

“导入”代码无效:

server.js

'use strict';
import Number from "./Number";

let sum = 0;


let numberObj = new Number();

sum = numberObj.addNumber(1,2);
console.log("sum of two number 1 and 2 "+ sum);

数字.js

'use strict';
export default class Number {

  addNumber(num1, num2) {
    return num1 + num2;
  }
}

我还检查了http://node.green/ 以查看支持的 es6,但无法理解为什么它不能与 --harmony_modules 选项一起使用。请帮忙。

【问题讨论】:

标签: javascript node.js ecmascript-6 es6-modules


【解决方案1】:

它们只是还没有实现。

Node 6.0.0 使用 V8 版本,完成了大部分 ES6 功能。不幸的是,模块不是那些已完成的功能之一。

node --v8-options | grep harmony 

进行中和谐标志未完全实施,通常无法正常工作:

--es_staging(启用值得测试的和谐功能(仅供内部使用))
--harmony(启用所有已完成的和声功能)
--harmony_shipping(启用所有已发布的和谐功能)
--harmony_object_observe(启用“和谐对象.observe”(进行中))
--harmony_modules(启用“和谐模块”(进行中)强>))
--harmony_function_sent(启用“harmony function.sent”(进行中))
--harmony_sharedarraybuffer(启用“harmony sharedarraybuffer”(进行中))
--harmony_simd(启用“和谐 simd”(进行中))
--harmony_do_expressions(启用“和谐执行表达式”(进行中))
--harmony_iterator_close(启用“和谐迭代器完成”(进行中))
--harmony_tailcalls(启用“和谐尾调用”(进行中))
--harmony_object_values_entries(启用“和谐 Object.values / Object.entries”(进行中))
--harmony_object_own_property_descriptors(启用“harmony Object.getOwnPropertyDescriptors()”(进行中))
--harmony_regexp_property(启用“harmony unicode regexp 属性类”(进行中))
--harmony_function_name(启用“和谐函数名称推断”)
--harmony_regexp_lookbehind(启用“和谐正则表达式后视”)
--harmony_species(启用“和谐符号.species”)
--harmony_instanceof(启用“和谐实例支持”)
--harmony_default_parameters(启用“和谐默认参数”)
--harmony_destructuring_assignment(启用“和谐解构赋值”)
--harmony_destructuring_bind(启用“和谐解构绑定”)
--harmony_tostring(启用“和谐 toString”)
--harmony_regexps(启用“和谐正则表达式扩展”)
--harmony_unicode_regexps(启用“和谐 unicode 正则表达式”)
--harmony_sloppy(启用“草率模式下的和谐功能”)
--harmony_sloppy_let(启用“和谐让马虎模式”)
--harmony_sloppy_function(启用“和谐草率功能块范围”)
--harmony_proxies(启用“和谐代理”)
--harmony_reflect(启用“和谐反射 API”)
--harmony_regexp_subclass(启用“和谐正则表达式子类化”)

【讨论】:

  • 谢谢。我看到了,但不相信“import”是 es6 的重要语法之一
  • @joy 是的,希望它很快就可以使用。
  • @KingWu 我刚刚在更新到node 6.0.0 后运行node --v8-options | grep harmony 找到它。
  • 我很高兴听到节点 6 退出了,现在他们没有实现最重要的功能之一...... :(
  • @SuperUberDuper 使用节点 7
【解决方案2】:

如上所述,ES6 模块尚未实现。

以一种与通用 JS 模块向后兼容的方式实现 ES6 模块似乎是一个不小的问题,这是当前的 Node.js 模块语法。

但是,有一个实现 draft,它为包含 ES6 模块的文件引入了一个新的文件扩展名 - .mjs

另外,counter-proposal 提供了一种替代方法,即在 package.json 中声明所有带有 ES6 模块的文件,如下所示:

{
    "modules.root": "/path/to/es6/modules"
}

【讨论】:

  • 我认为这是很重要的一点。 “The Powers That Be”仍在争论规范,然后它必须出现在 v8 中才能到达 Node。
  • 请注意:最新的草稿不需要.mjs作为扩展名,兼容性更好。
  • 问题是:我在哪里可以找到这些 es6 模块下载?
  • 这个blog post 可能是一个不错的起点。
【解决方案3】:

这应该是对@Paulpro 回答的评论,但我没有足够的代表发表评论。

对于 Windows 用户,等效命令为:

node --v8-options | findstr harmony

【讨论】:

    【解决方案4】:

    在实现模块之前,您可以使用the Babel "transpiler" 来运行您的代码:

    npm install --save babel-cli babel-preset-node6
    ./node_modules/.bin/babel-node --presets node6 ./your_script.js
    

    https://www.npmjs.com/package/babel-preset-node6https://babeljs.io/docs/usage/cli/

    缺点:这有很多缺点,例如额外的编译时间,这可能很重要,您现在需要源映射来进行调试;只是说。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-04
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多