3

I'm having trouble using the ESLint CLI with the --rule option.

# This is what I tried
eslint --rule "{no-console: error}" --fix-dry-run . 

Resulting in the following error:

Invalid value for option 'rule' - expected type Object, received value: {no-console:.

What is the right way of using the --rule option? I have ESLint installed locally and use npx to run it.

  • Node.js version 14.15.0
  • ESLint version 7.14.0
  • OS Windows 10

.eslintrc.js

module.exports = {
  env: {
    browser: true,
    es2021: true,
    node: true
  },
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:vue/vue3-recommended',
    'prettier',
    'prettier/vue'
  ],
  parser: 'vue-eslint-parser',
  parserOptions: {
    parser: '@typescript-eslint/parser',
    ecmaVersion: 12,
    sourceType: 'module'
  },
  plugins: ['@typescript-eslint'],
  rules: {
    '@typescript-eslint/explicit-module-boundary-types': 'off',
    '@typescript-eslint/no-explicit-any': 'off'
  }
};
Jens
  • 410
  • 3
  • 13

2 Answers2

4

It seems to be a bug with npx and Windows. Explained in this issue removing the spaces inside --rule solves the issue:

npx eslint --rule "no-console:error" --fix-dry-run .
Jens
  • 410
  • 3
  • 13
0

You could add "no-console": ["error"] in .eslintrc.js rules

module.exports = {
  ...
  rules: {
    '@typescript-eslint/explicit-module-boundary-types': 'off',
    '@typescript-eslint/no-explicit-any': 'off',
    'no-console': ['error']
  }
};

and just run

eslint --fix-dry-run ./

or

eslint --rule "no-console: ['error']" --fix-dry-run .

https://eslint.org/docs/user-guide/command-line-interface#-rule

Lashan Fernando
  • 1,187
  • 1
  • 7
  • 21
  • 1
    I'd like to apply the `no-console` rule only when running from the command line. However, doing `--rule "no-console: ['error']"` doesn't seem to work for me. Getting `No files matching the pattern "['error']" were found`. – Jens Nov 28 '20 at 14:47