ESLint Plugin for .js Extension Enforcement
The `eslint-plugin-require-extensions` package provides a straightforward ESLint plugin designed to ensure that relative import and export paths consistently include the `.js` file extension. This plugin addresses a specific pain point in TypeScript development where TypeScript itself does not automatically transform file extensions nor strictly enforce their presence in module resolution, particularly problematic for environments that require explicit extensions for ESM, such as Node.js. The current stable version is `0.1.3`, indicating an early stage of development, likely with infrequent releases focused on stability and specific enhancements. Its primary differentiator is its singular focus on this `.js` extension problem, providing a targeted and effective solution for projects aiming for full ESM compatibility and consistent module resolution across different environments. It's a pragmatic tool for maintaining import consistency that TypeScript currently leaves unaddressed, helping prevent runtime errors related to module resolution.
Common errors
-
Relative imports and exports must end with .js require-extensions/require-extensions
cause A relative import or export statement is missing the '.js' file extension.fixAdd the '.js' extension to all relative import and export paths (e.g., change `import Foo from './bar'` to `import Foo from './bar.js'`). You can run `eslint --fix .` to automatically fix most of these issues.
Warnings
- gotcha TypeScript's default behavior does not enforce or transform file extensions for relative imports, which can lead to runtime errors, especially in Node.js ESM environments that require explicit '.js' extensions. This plugin directly addresses that gap.
- gotcha Using this plugin in a mixed CommonJS/ESM codebase might lead to linting errors for CommonJS files if they use relative imports without `.js` extensions, even though CommonJS typically doesn't strictly require them. This plugin is primarily aimed at ESM consistency.
Install
-
npm install eslint-plugin-require-extensions -
yarn add eslint-plugin-require-extensions -
pnpm add eslint-plugin-require-extensions
Imports
- plugins
"plugins": [ "eslint-plugin-require-extensions" ]"plugins": [ "require-extensions" ] - extends (recommended)
"extends": [ "require-extensions/recommended" ]"extends": [ "plugin:require-extensions/recommended" ] - rules (individual)
"rules": { "require-extensions": "error" }"rules": { "require-extensions/require-extensions": "error" }
Quickstart
npm install --save-dev eslint-plugin-require-extensions
// .eslintrc.json
{
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:require-extensions/recommended"
],
"plugins": [
"require-extensions"
],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
}
}
// src/index.ts (example file)
// import SomeModule from './some-module'; // This would trigger an error
import OtherModule from './other-module.js'; // This is correct
// To run the linter and fix errors:
npx eslint src --fix