nodejs 8导入模块 - 需要还是导入? [英] nodejs 8 import module - require or import?

查看:149
本文介绍了nodejs 8导入模块 - 需要还是导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



或者我们仍然需要babel使用 import



我一直在挖掘但似乎没有答案。如果我们仍然需要使用require,为什么节点不能实现 import 呢?

解决方案

UPDATE-2017.11.01↓



简答

我们仍在使用要求



长答案

ESM加载部分落在节点 8.5.0 于9月发布。请参阅API文档此处。注意事项包括需要 - experimental-modules 标志以及使用新的 .mjs 扩展模块。您可以查看此处的完整讨论。 / p>

在ESM加载稳定且功能齐全之前,仍然需要在V8中进行更改以便与我的原始答案一致,我仍然建议坚持使用CommonJS 需要如果你还没有使用Babel用于其他东西



请参阅发布以获得更多教学解释



< hr>

以前的答案↓



这两种实现方式完全不同,所以它的内容远不止于此眼睛



外卖是关于规格仍然存在问题(一直到V8),因此 import 目前无法使用转换器在Node中实现



贡献者


在当前时间点,仍有在Node.js甚至可以开始构建可支持的ES6模块实现之前,需要在ES6和虚拟机方面发生的规范和实现问题的数量。工作正在进行中,但需要一些时间 - 我们目前至少要查看一年左右。


保持请注意,转换器只是将ES6模块语法转换为CommonJS模块语法,因此目前没有性能优势。换句话说,如果您还没有Babel管道,那么创建一个仅用于使用新建议的 import 语法的动机并不多,除了主动语法之外透视



有关实施方式不同的详细信息,看到这个写了


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

Or do we still need babel for using import?

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

解决方案

UPDATE-2017.11.01 ↓

Short answer
We're still using require

Long answer
ESM loading has partially landed in node 8.5.0 which was released in September. See the API documentation here. Caveats include the need for the --experimental-modules flag and the use of a new .mjs extension for modules. You can have a look at the complete discussion here

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 require if you don't already use Babel for other stuff

See this post for 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 import cannot currently be implemented in Node without a using a transpiler

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

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.

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 import syntax, except from a proactive syntactic perspective

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

这篇关于nodejs 8导入模块 - 需要还是导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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