Skip to content

Instantly share code, notes, and snippets.

@potados99
Created March 5, 2020 17:10
Show Gist options
  • Save potados99/28b833a58504b1c5f3a7006d5058d0e1 to your computer and use it in GitHub Desktop.
Save potados99/28b833a58504b1c5f3a7006d5058d0e1 to your computer and use it in GitHub Desktop.
Node.js v12 + ECMA Module

Enable ES module support

Built-in way

Use --experimental-modules.

For example:

node --experimental-modules index.js

More simple way

First install esm.

npm i esm

And run node with -r esm option.

node -r esm index.js

This also resolves an issue where node requires file extension of an ES module.

Treat files as ES module

In package.json, filed type, two options: commonjs(default) or module

  • If set to module: every .js files are treated as ES modules.
  • If set to commonjs: files with .mjs extension are treated as ES moduels.

If you want to use both CommonJS and ES modules, do not set type to module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment