eslint-cjs-to-esm
eslint-cjs-to-esm copied to clipboard
ESLint wrapper for migration from CJS to ESM.
eslint-cjs-to-esm
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
Install with npm:
npm install eslint-cjs-to-esm
Usage
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}"
Rules
This rule set is based on ESLint rules for migrating projects from CommonJS to ESM.
eslint-plugin-file-extension-in-import-ts
eslint-plugin-node
| ESLint Plugin | Rule | Source | Description | Fixable |
|---|---|---|---|---|
| node | no-extraneous-import | :link: | disallow import declarations which import extraneous modules | - |
| node | no-sync | :link: | disallow synchronous methods | - |
| node | file-extension-in-import | :link: | enforce the style of file extensions in import declarations |
Yes |
eslint-plugin-import
| ESLint Plugin | Rule | Source | Description | Fixable |
|---|---|---|---|---|
| import | extensions | :link: | Ensure consistent use of file extension within the import path. | - |
| import | no-unresolved | :link: | Ensure imports point to a file/module that can be resolved. | - |
| import | no-useless-path-segments | :link: | Prevent unnecessary path segments in import and require statements. | Yes |
| import | no-extraneous-dependencies | :link: | Forbid the use of extraneous packages. | - |
| import | no-commonjs | :link: | 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-unicorn
| ESLint Plugin | Rule | Source | Description | Fixable |
|---|---|---|---|---|
| unicorn | prefer-module | :link: | Prefer JavaScript modules (ESM) over CommonJS. | Yes |
| unicorn | prefer-node-protocol | :link: | Prefer using the node: protocol when importing Node.js builtin modules. |
Yes |
| unicorn | prefer-top-level-await | :link: | 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
Changelog
See Releases page.
Running tests
Install devDependencies and Run npm test:
npm test
Contributing
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
Author
License
MIT © azu