使用Node.js要求与ES6导入/导出 [英] Using Node.js require vs. ES6 import/export

查看:111
本文介绍了使用Node.js要求与ES6导入/导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在进行的一个项目中,我们有两个选择可以使用哪个模块系统:

In a project I'm collaborating on, we have two choices on which module system we can use:


  1. 使用require导入模块,并使用module.exports和exports.foo导出

  2. 使用ES6导入导入模块,并使用ES6导出导出

使用其他功能是否有任何性能优势?如果我们使用节点的ES6模块,我们应该知道什么呢?

Are there any performance benefits to using one over the other? Is there anything else that we should know if we were to use ES6 modules over Node ones?

推荐答案


使用其中一个是否有任何性能优势?

Are there any performance benefits to using one over the other?

请记住,没有JavaScript引擎,原生支持ES6模块。你说自己在使用Babel。 Babel将 import export 声明转换为CommonJS( require module.exports )默认情况下。因此,即使您使用ES6模块语法,如果您在Node中运行代码,则将使用CommonJS。

Keep in mind that there is no JavaScript engine yet that natively supports ES6 modules. You said yourself that you are using Babel. Babel converts import and export declaration to CommonJS (require/module.exports) by default anyway. So even if you use ES6 module syntax, you will be using CommonJS under the hood if you run the code in Node.

CommonJS和ES6模块之间存在技术上的区别,例如CommonJS允许您动态加载模块。 ES6不允许这样做,但是正在开发一个API

There are technical difference between CommonJS and ES6 modules, e.g. CommonJS allows you to load modules dynamically. ES6 doesn't allow this, but there is an API in development for that.

由于ES6模块是标准的一部分,我将使用它们。

Since ES6 modules are part of the standard, I would use them.

这篇关于使用Node.js要求与ES6导入/导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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