How to use it
- Choose JavaScript or TypeScript and where the code runs.
- Say whether
.jsfiles useimportorrequire. - Tick the rules you want and, if you format with Prettier, the Prettier option.
- Run the
npm install --save-dev …command from the first line, save the file in the project root and runnpx eslint ..
How it works
The output is an array exported with export default:
- an object with only
ignores— global ignores for build folders; js.configs.recommendedfrom@eslint/js(and...tseslint.configs.recommendedfor TypeScript);- your block: the file patterns,
ecmaVersion: 'latest', the globals and the chosen rules; - for CommonJS, a block that sets
sourceType: 'commonjs'for.jsfiles (ESLint already treats.cjsas CommonJS); eslintConfigPrettierlast, if chosen.
Example
The defaults (JavaScript, browser, ES modules, recommended rules, eqeqeq, prefer-const, unused variables as warnings) give:
// npm install --save-dev eslint @eslint/js globals
import js from '@eslint/js';
import globals from 'globals';
export default [
{ ignores: ['dist/', 'build/', 'coverage/'] },
js.configs.recommended,
{
files: ['**/*.{js,mjs,cjs}'],
languageOptions: {
ecmaVersion: 'latest',
globals: globals.browser,
},
rules: {
eqeqeq: ['error', 'always'],
'prefer-const': 'error',
'no-unused-vars': 'warn',
},
},
];
Tips
- Add
"lint": "eslint ."to the scripts in package.json so everyone runs the same command. npx eslint . --fixrepairs what can be fixed automatically, such asletthat should beconst.- Let Prettier handle formatting and ESLint handle code quality; that is what the Prettier option is for.