eslint-plugin-import-esm

raw JSON →
3.0.1 verified Sat Apr 25 auth: no javascript

ESLint plugin v3.0.1 that enforces import/export paths to follow native ESM resolution by requiring explicit file extensions (e.g., '.mjs', '.js'). Works exclusively with ESLint v9+ flat config and is fully ESM. Released as stable; active development. Unlike eslint-plugin-import which validates path existence, this focuses on explicit-extension rule to prevent ambiguous ESM resolution. Automatically fixable via --fix.

error Error: Cannot find module 'eslint-plugin-import-esm'
cause Missing dependency or incorrect installation.
fix
Run 'npm install eslint-plugin-import-esm --save-dev' and ensure it's in package.json dependencies.
error Error: Plugin could not be loaded: eslint-plugin-import-esm. It requires ESLint v9 or higher.
cause Using ESLint version below 9.0.0.
fix
Update ESLint to v9+ with 'npm install eslint@9 --save-dev'.
error Error [ERR_REQUIRE_ESM]: require() of ES Module ... from ... not supported.
cause Attempting to load the plugin with require() instead of import.
fix
Use 'import importEsmPlugin from 'eslint-plugin-import-esm'' in an ESM context.
error Invalid configuration: Rule 'explicit-extension' not found.
cause Using the rule name without the 'import-esm/' prefix.
fix
Use 'import-esm/explicit-extension' in the rules object.
breaking Plugin requires ESLint v9+ with flat config; does not support .eslintrc or legacy config.
fix Migrate to ESLint flat config. Use defineConfig from 'eslint/config' and apply plugin configs as shown.
breaking Plugin is ESM-only; cannot be loaded with require().
fix Use import statement in an ESM context (module type in package.json or .mjs file).
breaking Node.js >= 18.0.0 required; does not work on older versions.
fix Upgrade Node.js to version 18.0.0 or newer.
gotcha Rule name must be prefixed with 'import-esm/' when configuring in rules object.
fix Use 'import-esm/explicit-extension' instead of 'explicit-extension'.
gotcha The 'explicit-extension' rule requires an option object with 'extension' property. Default extension is '.js' if not specified.
fix Provide configuration: ['error', { extension: '.mjs' }] or ['warn', { extension: '.js' }].
npm install eslint-plugin-import-esm
yarn add eslint-plugin-import-esm
pnpm add eslint-plugin-import-esm

Configures ESLint v9 flat config with recommended preset and explicit .mjs extension rule.

import { defineConfig } from "eslint/config";
import importEsmPlugin from 'eslint-plugin-import-esm';

export default defineConfig([
  importEsmPlugin.configs.recommended,
  {
    rules: {
      'import-esm/explicit-extension': ['error', { extension: '.mjs' }]
    }
  }
]);