kpimov kpimov - 4 months ago 343
Node.js Question

Using Node.js require vs. ES6 import/export

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


  1. Importing modules using require, and exporting using module.exports and exports.foo

  2. Importing modules using ES6 import, and exporting using ES6 export



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?

Answer

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

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.

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.

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

Comments