Since Node v12 (April 2019), support for ES modules is enabled by default, and since Node v15 (October 2020) it’s stable (see here). Files including node modules must either end in
.mjs or the nearest
package.json file must contain
"type": "module". The Node documentation has a ton more information, also about interop between CommonJS and ES modules.
Performance-wise there is always the chance that newer features are not as well optimized as existing features. However, since module files are only evaluated once, the performance aspect can probably be ignored. In the end you have to run benchmarks to get a definite answer anyway.
ES modules can be loaded dynamically via the
import() function. Unlike
require, this returns a promise.
Are there any performance benefits to using one over the other?
export declaration to CommonJS (
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 differences 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.