nodejs 8 导入模块 - 需要还是导入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44985913/
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-09-02 18:21:04  来源:igfitidea点击:

nodejs 8 import module - require or import?

node.jsimportbabel

提问by laukok

Just wonder how do we import a module in node.js 8 - are we still using require?

只是想知道我们如何在 node.js 8 中导入模块 - 我们还在使用require吗?

Or do we still need babel for using import?

还是我们仍然需要 babel 来使用import

I have been digging around but seems no answer. If we still have to use require, why can't node implement importyet?

我一直在挖掘,但似乎没有答案。如果还得用require,为什么nodeimport还不能实现呢?

回答by kuzyn

UPDATE-2018.11.15 ↓

更新-2018.11.15 ↓

Short answer
We're still using require

简短的回答
我们仍在使用require

Long answer
ESM loading has partially landed in node 8.5.0which was released in September 2017. As such, it has beeen part of the specs as an experimental feature for a little while: see the API documentation here. Caveats include the need for the --experimental-modulesflag and the use of a new .mjsextension for modules.

长答案
ESM 加载已部分登陆节点8.5.0,该节点于 2017 年 9 月发布。因此,它作为实验性功能一直是规范的一部分:请参阅此处的 API 文档。注意事项包括需要--experimental-modules标志和使用新.mjs的模块扩展。

There is still changes that need to happen in V8 before ESM loading is stable and fully featured so as with my original answer, I would still advise on sticking with CommonJS requireif you don't already use Babel for other stuff

在 ESM 加载稳定且功能齐全之前,V8 中仍然需要进行一些更改,因此与我的原始答案一样,require如果您还没有将 Babel 用于其他内容,我仍然建议您坚持使用 CommonJS

See this postfor a more didactic explanation

有关更多教学解释,请参阅此帖子



PREVIOUS ANSWER ↓

以前的回答↓

The two implementations are completely different under the hood, so there is more to it than what meets the eyes

这两种实现在幕后完全不同,所以它比眼睛看到的更多

The takeaway is that there are still lingering issues/questions over the specifications (all the way to V8), and as such importcannot currently be implemented in Node without a using a transpiler

要点是规范(一直到 V8)仍然存在挥之不去的问题/疑问,因此,import如果不使用转译器,目前无法在 Node 中实现

See this comment (dated February 2017) from one of the contributor:

请参阅其中一位贡献者的评论(日期为 2017 年 2 月):

At the current point in time, there are still a number of specification and implementation issues that need to happen on the ES6 and Virtual Machine side of things before Node.js can even begin working up a supportable implementation of ES6 modules. Work is in progress but it is going to take some time?—?We're currently looking at around a year at least.

目前,在 Node.js 甚至可以开始研究 ES6 模块的可支持实现之前,ES6 和虚拟机方面仍有许多规范和实现问题需要解决。工作正在进行中,但需要一些时间?-?我们目前至少在一年左右。

Keep in mind that transpilers simply converts the ES6 module syntax to the CommonJS module syntax, so there is currently no performance benefits. In other words, if you don't have a Babel pipeline already, there is not much incentives to create one just to use the new proposed importsyntax, except from a proactive syntactic perspective

请记住,转译器只是将 ES6 模块语法转换为 CommonJS 模块语法,因此目前没有性能优势。换句话说,如果你还没有 Babel 管道,那么创建一个只是为了使用新提议的import语法,除了从积极的语法角度来看,没有太多的动力

For more details on how the implementation differs, see this write up

有关实现有何不同的更多详细信息,请参阅此文章