ESLint wrapper for migration from CJS to ESM.
This tool's main use-case is to migrate from CommonJS (JavaScript/TypeScript) to ES Modules (JavaScript/TypeScript).
This tool is a wrapper of ESLint, and it built-in some rules for migration. So, you can just use this tool without any configuration.
Install with npm:
npm install eslint-cjs-to-esm
Command Line Arguments are the same as those of ESLint.
npx eslint-cjs-to-esm [ESLint Arguments!]
Run Lint
npx eslint-cjs-to-esm "./src/**/*.{js,ts}"
Fix Errors
npx eslint-cjs-to-esm "./src/**/*.{js,ts}" --fix
Warning You need to start with
.
for relative path. It is wrapper limitation.
NG:npx eslint-cjs-to-esm "src/**/*.ts"
OK:npx eslint-cjs-to-esm "./src/**/*.ts"
DEBUG:
DEBUG=eslint-cjs-to-esm npx eslint-cjs-to-esm "./src/**/*.{js,ts}"
This rule set is based on ESLint rules for migrating projects from CommonJS to ESM.
ESLint Plugin | Rule | Source | Description | Fixable |
---|---|---|---|---|
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.* . |
- |
📝 commonjs-to-es-module-codemod helps you to migrate from require
/exports
(CJS) to import
/export
(ESM).
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 |
- ESLint rules for migrating projects from CommonJS to ESM
- Make "import/extensions" require the .js extension in a Node.js TypeScript project - Stack Overflow
See Releases page.
Install devDependencies and Run npm test
:
npm test
Pull requests and stars are always welcome.
For bugs and feature requests, please create an issue.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
MIT © azu