45

I'm using eslint in visual code to format the js files but it gives me errors everytime i run it, here's my command line to run it node ./node_modules/eslint/bin/eslint -- src --fix the error message

E:\PATH\src\reducers\roote-reducer.js
  1:8   error  There should be no space after '{'                object-curly-spacing
  1:26  error  There should be no space before '}'               object-curly-spacing
  6:1   error  Expected indentation of 1 tab but found 2 spaces  indent
  7:1   error  Expected indentation of 1 tab but found 2 spaces  indent
  8:4   error  Newline required at end of file but not found     eol-last

E:\PATH\src\reducers\schedule-reducer.js
   1:8   error  There should be no space after '{'                  object-curly-spacing
   1:22  error  There should be no space before '}'                 object-curly-spacing
   4:1   error  Expected indentation of 1 tab but found 4 spaces    indent
   7:24  error  Missing space before function parentheses           space-before-function-paren
   7:54  error  Missing space before opening brace                  space-before-blocks
   8:1   error  Expected indentation of 1 tab but found 4 spaces    indent
   9:1   error  Expected indentation of 1 tab but found 4 spaces    indent

E:\PATH\src\register-service-worker.js
   12:1  error  Expected indentation of 1 tab but found 2 spaces    indent
   17:1  error  Expected indentation of 5 tabs but found 6 spaces   indent
   17:7  error  Use regex shorthands to improve readability         unicorn/regex-shorthand
   22:1  error  Expected indentation of 1 tab but found 2 spaces    indent

✖ 309 problems (309 errors, 0 warnings)
  309 errors, 0 warnings potentially fixable with the `--fix` option.

How can i fix them automatically?

gretty volk
  • 591
  • 1
  • 4
  • 10

12 Answers12

63

To use --fix option, you need to run eslint directly.

Try this

./node_modules/.bin/eslint src --fix

On Windows:

.\node_modules\.bin\eslint src\** --fix
Mr. Doge
  • 796
  • 5
  • 11
piuspbd
  • 993
  • 7
  • 16
15

The format should be:

./node_modules/.bin/eslint --fix path/to/file.js

See this thread (and aravind1078's answer) for more background.

CodeBiker
  • 2,985
  • 2
  • 33
  • 36
11

Run the below command in the new terminal:

./node_modules/.bin/eslint --fix . --ext .js,.vue src

Aeolian7
  • 326
  • 4
  • 9
8

Try to run

eslint --fix

you have to install first eslint

npm install -g eslint
Rohan
  • 640
  • 7
  • 11
6

in the directory in which you code exists run :

eslint --fix .

instead of

eslint --fix 

as the command needs an argument which indicates the directory in which the code exists or the file itself you want to apply linting on

Ahmed Maher
  • 1,521
  • 17
  • 22
  • I had to specify the file(s) explicitly, e.g. `eslint --fix src/path/some.js`. The above didn't scan the directories for files. – armandino Jan 18 '23 at 21:28
6

Use Command-Line Terminal, to run the below commands in Visual Studio Code.

Make sure .eslintrc.yml file configured for the working project

To fix lint issues in a file

npx eslint file1.js --fix

To fix lint issues in all the files in the folder

npx eslint ./folder_name --fix
4
  • I had difficulties in using --fix when I use eslint-config-airbnb-base instead of @nuxtjs/eslint-module in Nuxt.js project
  • At that time running eslint direclty worked for me: ./node_modules/.bin/eslint --fix


However, my preferred one is to write two more scripts into package.json

"lint": "eslint --ext \".js,.vue\" --ignore-path .gitignore .",
"lint-fix": "npm run lint -- --fix"

And run yarn lint-fix or npm run lint-fix

3

First install eslint if you already haven't

npm install -g eslint

Then run

eslint --fix path/to/file.extension


Note: You will have to run the the above command every time after making changes to that particular file.

av123
  • 31
  • 3
2

If it didn't

extend(config, ctx) {
      // Run ESLint on save
      if (ctx.isDev && ctx.isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/,
          options: {
            fix: true
          }
        })
      }
    }
You. You can use this on the first page
    const colors = require('vuetify/es5/util/colors').default
    const pkg = require('./package')
    require('dotenv').config()

    module.exports = {
      mode: 'universal',
      /*
      ** Headers of the page
      */
      head: {
        titleTemplate: '%s - ' + process.env.npm_package_name,
        title: process.env.npm_package_name || '',
        meta: [
          { charset: 'utf-8' },
          { name: 'viewport', content: 'width=device-width, initial-scale=1' },
          {
            hid: 'description',
            name: 'description',
            content: process.env.npm_package_description || ''
          }
        ],
        link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }]
      },
      /*
      ** Customize the progress-bar color
      */
      loading: { color: '#fff' },
      /*
      ** Global CSS
      */
      css: [],
      /*
      ** Plugins to load before mounting the App
      */
      plugins: [],
      /*
      ** Nuxt.js dev-modules
      */
      buildModules: [
        // Doc: https://github.com/nuxt-community/eslint-module
        '@nuxtjs/eslint-module',
        '@nuxtjs/vuetify'
      ],
      /*
      ** Nuxt.js modules
      */
      modules: [
        // Doc: https://axios.nuxtjs.org/usage
        '@nuxtjs/axios',
        '@nuxtjs/pwa',
        // Doc: https://github.com/nuxt-community/dotenv-module
        '@nuxtjs/dotenv'
      ],
      /*
      ** Axios module configuration
      ** See https://axios.nuxtjs.org/options
      */
      axios: {},
      /*
      ** vuetify module configuration
      ** https://github.com/nuxt-community/vuetify-module
      */
      /*
      ** Build configuration
      */
      build: {
        extend(config, ctx) {
          // Run ESLint on save
          if (ctx.isDev && ctx.isClient) {
            config.module.rules.push({
              enforce: 'pre',
              test: /\.(js|vue)$/,
              loader: 'eslint-loader',
              exclude: /(node_modules)/,
              options: {
                fix: true
              }
            })
          }
        }
      }
    }

If you still don't understand me.

Mm.Mirzaei.dev
  • 333
  • 3
  • 10
1

I just had the same problem I run

eslint --fix fileName.extention
1

First Install Package: npm install -g eslint

and

Run: eslint --fix

1

if it doesn't fix any error with the '--fix' flag, it means your ESlint config file has an error. please cross check the file again

s__mc
  • 42
  • 4