Javascript ES2015“导入”在带有 --harmony_modules 选项的节点 v6.0.0 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36901147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:41:38  来源:igfitidea点击:

ES2015 "import" not working in node v6.0.0 with with --harmony_modules option

javascriptnode.jsecmascript-6es6-modules

提问by joy

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_modulesoption as well but still got the same error about "import". Here's the code.

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

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

服务器.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

数字.js

'use strict';
export default class Number {

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

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.

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

回答by Paul

They're just not implemented yet.

它们只是尚未实施。

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 6.0.0 使用了一个 V8 版本,完成了大部分 ES6 功能。不幸的是,模块不是那些已完成的功能之一。

node --v8-options | grep harmony 

in progressharmony flags are not fully implemented and usually are not working:

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

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

--es_staging(启用值得测试的和声功能(仅供内部使用))
--harmony(启用所有已完成的和声功能)
--harmony_shipping(启用所有附带的和声功能)
--harmony_object_observe(启用“harmony Object.observe”(在进展))
--harmony_modules(启用“和谐模块”(进行中))
--harmony_function_sent(启用“和谐function.sent”(进行中))
--harmony_sharedarraybuffer(启用“和谐sharedarraybuffer”(进行中))
--harmony_simd (启用“和谐 simd”(进行中))
--harmony_do_expressions(启用“harmony do-expressions”(进行中))
--harmony_iterator_close(启用“harmony iterator finalization”(进行中))
--harmony_tailcalls(启用“harmony tail call”(进行中))
--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 Symbol.species”)-- harmony_instanceof (启用“harmony instanceof support”)
--harmony_default_parameters(启用“harmony 默认参数”)
--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 代理”)
--harmony_reflect(启用“harmony Reflect API”)
--harmony_regexp_subclass(启用“harmony regexp subclassing”)

回答by CodeVortex

This should be a comment to @Paulpro's answer but I do not have enough rep to post a comment.

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

For Windowsusers the equivalent command is:

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

node --v8-options | findstr harmony

回答by wires

Until modules are implemented you can use the Babel "transpiler"to run your code:

在实现模块之前,您可以使用Babel“转译器”来运行您的代码:

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

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

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

Downsides: this has various downsides, such as extra compilation time, which can be significant and you now need source maps for debugging; just saying.

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

回答by Роман Парадеев

As is stated above, ES6 modules are not implemented yet.

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

It appears to be a non-trivial issue to implement ES6 modules in a way that would be backward-compatible with Common JS modules, which is the current Node.js module syntax.

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

However, there is a draftof an implementation, that introduces a new file extension - .mjs- for a files containing ES6 modules.

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

Also, there is a counter-proposalthat present an alternative approach of declaring all files with ES6 modules in package.json like so:

此外,还有一个反提案提出了一种在 package.json 中使用 ES6 模块声明所有文件的替代方法,如下所示:

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