To enable es module system in nodejs, it's not as simple as enabling the --experimental-modules
flag.
You need:
- use
.mjs
file format to hint as a es module or at the nearestpackage.json
, specify"type": "module"
- use
import
statement to "require" the module file -> meaning your main file also need to be a.mjs
file. - to replace all "require" and "module.exports" in
.mjs
file toimport
. - es modules import file must specify extension,
.mjs
(es modules),.cjs
(commonjs) or.js
(depends on nearestpackage.json#type
). - for commonjs to import es modules, the only way is to use dynamic import
import()
.