The ESM standard is considered stable in NodeJS and well supported by a lot of modern JavaScript tools.
ESLint does a good job validating and fixing ESM code (as long as you don't use top-level await, coming in ESLint v8). Make sure to enable the latest ECMA features in the ESLint config.
- .eslint.json
{
"env": {
"es2021": true,
"node": true
},
"parserOptions": {
"sourceType": "module"
}
}
Rules that can be helpful for anyone who wants to migrate a project from CommonJS to ESM now:
ESLint Plugin | Rule | Source | Description | Fixable |
---|---|---|---|---|
node | no-missing-import | π | disallow import declarations which import non-existence modules |
- |
node | no-extraneous-import | π | disallow import declarations which import extraneous modules | - |
node | no-sync | π | disallow synchronous methods | - |
node | file-extension-in-import | π | enforce the style of file extensions in import declarations |
Yes |
ESLint Plugin | Rule | Source | Description | Fixable |
---|---|---|---|---|
import | extensions | π | Ensure consistent use of file extension within the import path. | - |
import | no-unresolved | π | Ensure imports point to a file/module that can be resolved. | - |
import | no-useless-path-segments | π | Prevent unnecessary path segments in import and require statements. | Yes |
import | no-extraneous-dependencies | π | Forbid the use of extraneous packages. | - |
import | no-commonjs | π | Report CommonJS require calls and module.exports or exports.* . |
- |
ESLint Plugin | Rule | Source | Description | Fixable |
---|---|---|---|---|
unicorn | prefer-module | π | Prefer JavaScript modules (ESM) over CommonJS. | Yes |
unicorn | prefer-node-protocol | π | Prefer using the node: protocol when importing Node.js builtin modules. |
Yes |
unicorn | prefer-top-level-await | π | Prefer top-level await over top-level promises and async function calls. | Suggest |
- sindresorhus - Pure ESM package
- sindresorhus - Get Ready For ESM
- Node documentation - ECMAScript modules
Thank you very much for this very useful info! π