Skip to content

azu/eslint-cjs-to-esm

Repository files navigation

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 🔗 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-import

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-unicorn

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

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.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

License

MIT © azu