1

For eslint.config.js (flat config), what command should I use when linting *.js files recursively from the command line? 1


With eslintrc.js or eslintrc.json, I have been using npx eslint . --ext .js, as suggested here. Until now, it always worked fine.

But when I try that with eslint.config.js, the response in the command line is :

Invalid option '--ext' - perhaps you meant '-c'? You're using eslint.config.js, some command line flags are no longer available. Please see https://eslint.org/docs/latest/use/command-line-interface for details.

I didn't find any solution at the suggested link.

My eslint.config.js is in the directory where I try to run the lint command. 2

import eslintJs from '@eslint/js';
import globals from 'globals';

export default [
  eslintJs.configs.recommended,
  {
    // files: ['**/*.js', '**/*.cjs', '**/*.mjs'], // = default, not needed
    languageOptions: { globals: { ...globals.node } },
    rules: { 'no-unused-vars': ['warn', { argsIgnorePattern: '^_' }] },
  },
];

package.json :

{
  "name": "flat-config",
  "devDependencies": {
    "eslint": "^8.52.0"
  },
  "private": true,
  "type": "module"
}

I ran npm install before running npx eslint . --ext .js.

For demo purposes, I have a small bad-code.js file :

const hiUnused = 'hi there';
const hello='Hello world';
const unusedFunc = function(unusedVariable) { console.log('hi!') }
consoler.log(hello);

If and when ESLint works, I expect it to respond with three warnings and one error.

warning  'hiUnused'       never used
warning  'unusedFunc'     never used
warning  'unusedVariable' never used
 error   'consoler'       not defined

1 I include the tag because I expect the corresponding question and answer(s) to be very similar for TypeScript.

2 I include the line files: ['**/*.js', '**/*.cjs', '**/*.mjs'], to indicate that by default, ESLint matches **/*.js, **/*.cjs, and **/*.mjs. Commenting out or leaving it in should therefore not make any difference.

1 Answer 1

5

What command should I use when linting *.js files recursively from the command line?

Try : 1

npx eslint **/*.js

This will lint all *.js files in the current directory and all its subdirectories.
The command npx eslint **/*.js works just as fine for the old-school .eslintrc.* type of files as it does for the new eslint.config.js ones.

For your demo file, bad-code.js, ESLint responds :

1:7   warning  'hiUnused' is assigned a value but never used    no-unused-vars
3:7   warning  'unusedFunc' is assigned a value but never used  no-unused-vars
3:29  warning  'unusedVariable' is defined but never used.
                  Allowed unused args must match /^_/u          no-unused-vars
4:1   error    'consoler' is not defined                        no-undef

ESLint responds with three warnings and one error.

^ click to enlarge


1 This is from the documentation. Just filter out all the config stuff.
(Disregard ESLINT_USE_FLAT_CONFIG=true and --config some-other-file.js.)

Not the answer you're looking for? Browse other questions tagged or ask your own question.