ESLint
ESLint is a powerful, pluggable tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, enforcing coding standards and catching potential issues. It uses an AST-based parser and is entirely pluggable. The current stable version is v10.2.1. It follows a frequent release cadence for minor features and bug fixes, with major versions introducing breaking changes.
Common errors
-
ESLint: Node.js v18.x is no longer supported. Please upgrade to a supported version.
cause Running ESLint v10 on an unsupported Node.js version (e.g., Node.js 18).fixUpgrade your Node.js installation to v20.19.0, v22.13.0, or >=24. -
TypeError: ESLint is not a constructor
cause Attempting to use `require('eslint').ESLint` in an ESM context, or incorrectly mixing CJS and ESM, or not importing it correctly.fixEnsure you are using `import { ESLint } from 'eslint';` in an ESM module context. -
ERR_REQUIRE_ESM: Must use import to load ES Module: .../node_modules/eslint/lib/eslint/eslint.js
cause Attempting to `require()` an ESLint module that is ESM-only, or trying to load an `eslint.config.js` file with `require` syntax.fixConvert your project or specific file to use ESM `import` statements, and ensure your `eslint.config.js` is also ESM. -
Error: Cannot find module '@eslint/js/configs/eslint-recommended'
cause Occurs with `pnpm` if peer dependencies are not correctly hoisted or automatically installed.fixAdd `auto-install-peers=true` and `node-linker=hoisted` to your `.npmrc` file and reinstall dependencies. -
TS2307: Cannot find module 'eslint' or its corresponding type declarations.
cause TypeScript version is too old for ESLint's type definitions, or `eslint` is not correctly installed.fixUpdate TypeScript to version 5.3 or later, or ensure `eslint` is properly installed in your `node_modules`.
Warnings
- breaking ESLint v10 requires Node.js v20.19.0, v22.13.0, or >=24. Older Node.js versions are not supported.
- breaking Configuration files (`eslint.config.js`) must be written in ESM format (`import/export` syntax). CommonJS (`require/module.exports`) is no longer supported for config files.
- breaking Programmatic usage of ESLint, including the `ESLint` class, is primarily designed for ESM environments. While a CJS entry point may exist, it is largely deprecated and can lead to module resolution issues.
- gotcha Using `pnpm` requires specific `.npmrc` settings (`auto-install-peers=true`, `node-linker=hoisted`) to avoid dependency resolution errors related to peer dependencies.
- gotcha If you use ESLint's TypeScript type definitions, TypeScript 5.3 or later is required.
Install
-
npm install eslint -
yarn add eslint -
pnpm add eslint
Imports
- ESLint
const { ESLint } = require('eslint');import { ESLint } from 'eslint';
Quickstart
import { ESLint } from 'eslint';
async function lintCode(code: string): Promise<void> {
const eslint = new ESLint({
useEslintrc: false, // Don't use .eslintrc.* files
overrideConfigFile: true, // Use this config file
overrideConfig: {
languageOptions: {
ecmaVersion: 2022,
sourceType: "module",
globals: {
console: "readonly"
}
},
rules: {
"no-unused-vars": "warn",
"prefer-const": "error"
}
}
});
const results = await eslint.lintText(code);
console.log(`Linting results for: "${code}"`);
results.forEach(result => {
result.messages.forEach(message => {
console.log(
` Line ${message.line}, Col ${message.column}: ${message.message} (${message.ruleId})`
);
});
});
}
lintCode(`
var x = 1; // no-unused-vars (warn), prefer-const (error)
console.log('hello');
const y = 2;
`).catch(console.error);