Skip to content

Latest commit

 

History

History
24 lines (17 loc) · 936 Bytes

tell-node-to-treat-js-files-as-esm.md

File metadata and controls

24 lines (17 loc) · 936 Bytes

Tell Node To Treat JS Files As ESM

By default, Node will treat all .js files as CommonJS. That means you'll need to use the CommonJS export and require syntax to share code between files. If you'd instead like to opt-in to ESM (ECMAScript Modules), you'll need to update your package.json.

Add the following line:

"type": "module",

This will tell node that instead of CJS, it should treat all .js files as ESM. This means that ESM-based export and import syntax will work.

This also means you don't need to be defining your files with the .mjs extension.

For more extensive reading on this, see: