ES2015“导入"无法在节点 v6.0.0 中使用 --harmony_modules 选项 [英] ES2015 "import" not working in node v6.0.0 with with --harmony_modules option

查看:12
本文介绍了ES2015“导入"无法在节点 v6.0.0 中使用 --harmony_modules 选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using node v6.0.0 and wanted to use ES2016 (ES6). However I realized that the "import" syntax is not working. Isn't "import" fundamental to for writing modular code in ES2015? I tried running node with --harmony_modules option as well but still got the same error about "import". Here's the code.

没有导入"的工作代码:

Working code without "import":

'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);

带有导入"的无效代码:

Notworking code with "import":

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);

Number.js

'use strict';
export default class Number {

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

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

I also checked http://node.green/ to see the supported es6 however not able to understand why it doesn't work with --harmony_modules option. Please help.

推荐答案

他们只是还没有实施.

Node 6.0.0 使用 V8 版本,其中大部分 ES6 功能已完成.不幸的是,模块不是这些已完成的功能之一.

Node 6.0.0 uses a version of V8 with most of ES6 features completed. Unfortunately modules isn't one of those completed features.

node --v8-options | grep harmony 

进行中 和谐标志没有完全实现,通常不起作用:

in progress harmony flags are not fully implemented and usually are not working:

--es_staging(启用值得测试的和声功能(仅供内部使用))
--harmony(启用所有已完成的和声功能)
--harmony_shipping(启用所有附带的和声功能)
--harmony_object_observe(启用harmony Object.observe"(进行中))
--harmony_modules(启用和谐模块"(进行中))
--harmony_function_sent(启用harmony function.sent"(进行中))
--harmony_sharedarraybuffer(启用harmony sharedarraybuffer"(进行中))
--harmony_simd(启用harmony simd"(进行中))
--harmony_do_expressions(启用harmony do-expressions"(进行中))
--harmony_iterator_close(启用和谐迭代器完成"(进行中))
--harmony_tailcalls(启用和谐尾调用"(进行中))
--harmony_object_values_entries(启用harmony 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 regexp lookbehind")
--harmony_species(启用和谐符号.物种")
--harmony_instanceof(启用harmony instanceof support")
--harmony_default_parameters(启用和谐默认参数")
--harmony_destructuring_assignment(启用和谐解构赋值")
--harmony_destructuring_bind(启用和谐解构绑定")
--harmony_tostring(启用和声 toString")
--harmony_regexps(启用和谐正则表达式扩展")
--harmony_unicode_regexps(启用和谐unicode regexps")
--harmony_sloppy(启用草率模式下的和谐功能")
--harmony_sloppy_let(启用草率模式下的和谐")
--harmony_sloppy_function(启用harmony sloppy 功能块范围")
--harmony_proxies(启用和谐代理")
--harmony_reflect(启用和谐反射 API")
--harmony_regexp_subclass(启用和谐正则表达式子类化")

--es_staging (enable test-worthy harmony features (for internal use only))
--harmony (enable all completed harmony features)
--harmony_shipping (enable all shipped harmony features)
--harmony_object_observe (enable "harmony Object.observe" (in progress))
--harmony_modules (enable "harmony modules" (in progress))
--harmony_function_sent (enable "harmony function.sent" (in progress))
--harmony_sharedarraybuffer (enable "harmony sharedarraybuffer" (in progress))
--harmony_simd (enable "harmony simd" (in progress))
--harmony_do_expressions (enable "harmony do-expressions" (in progress))
--harmony_iterator_close (enable "harmony iterator finalization" (in progress))
--harmony_tailcalls (enable "harmony tail calls" (in progress))
--harmony_object_values_entries (enable "harmony Object.values / Object.entries" (in progress))
--harmony_object_own_property_descriptors (enable "harmony Object.getOwnPropertyDescriptors()" (in progress))
--harmony_regexp_property (enable "harmony unicode regexp property classes" (in progress))
--harmony_function_name (enable "harmony Function name inference")
--harmony_regexp_lookbehind (enable "harmony regexp lookbehind")
--harmony_species (enable "harmony Symbol.species")
--harmony_instanceof (enable "harmony instanceof support")
--harmony_default_parameters (enable "harmony default parameters")
--harmony_destructuring_assignment (enable "harmony destructuring assignment")
--harmony_destructuring_bind (enable "harmony destructuring bind")
--harmony_tostring (enable "harmony toString")
--harmony_regexps (enable "harmony regular expression extensions")
--harmony_unicode_regexps (enable "harmony unicode regexps")
--harmony_sloppy (enable "harmony features in sloppy mode")
--harmony_sloppy_let (enable "harmony let in sloppy mode")
--harmony_sloppy_function (enable "harmony sloppy function block scoping")
--harmony_proxies (enable "harmony proxies")
--harmony_reflect (enable "harmony Reflect API")
--harmony_regexp_subclass (enable "harmony regexp subclassing")

这篇关于ES2015“导入"无法在节点 v6.0.0 中使用 --harmony_modules 选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆