How to run ES Modules with Node.js

Given ECMAScript module index.mjs:

// index.mjs
import util from 'util';
console.log(util.inspect('Hello, World!'));

To execute index.mjs with Node.js v12, pass the --experimental-modules flag:

$ node --experimental-modules index.mjs
(node:84000) ExperimentalWarning: The ESM module loader is experimental.
'Hello, World!'

For Node.js v14 and up, the --experimental-modules flag isn’t necessary:

$ node index.mjs
'Hello, World!'

Please support this site and join our Discord!