174

I am trying to add lint-fix in my package.json. My basic lint is "lint": "eslint --ext .js,.vue src test/unit/specs test/e2e/specs"

I did try "lint-fix": "eslint --fix --ext .js,.vue src test/unit/specs test/e2e/specs" but it is throwing my errors but not fixing them.

What do I have to change?

The error from NPM is:

217 problems (217 errors, 0 warnings)
npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run-script" "lint-fix"
npm ERR! node v5.10.1
npm ERR! npm v3.8.3
npm ERR! code ELIFECYCLE
npm ERR! quasar-app@0.0.1 lint-fix: eslint --fix --ext .js,.vue src
npm ERR! Exit status 1
npm ERR! but it is caused by exit code 1 which is result of finding errors.
cartant
  • 57,105
  • 17
  • 163
  • 197
Marek Urbanowicz
  • 12,659
  • 16
  • 62
  • 87
  • can you share error message/ stack trace? – Gyandeep Oct 26 '16 at 22:03
  • @Gyandeep - list of errors, 217 problems (217 errors, 0 warnings) npm ERR! Windows_NT 10.0.14393 npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run-script" "lint-fix" npm ERR! node v5.10.1 npm ERR! npm v3.8.3 npm ERR! code ELIFECYCLE npm ERR! quasar-app@0.0.1 lint-fix: `eslint --fix --ext .js,.vue src` npm ERR! Exit status 1 npm ERR! but it is caused by exit code 1 which is result of finding errors. – Marek Urbanowicz Oct 26 '16 at 22:06

6 Answers6

307

You can run below command:

npm run lint -- --fix
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
kallayya Hiremath
  • 3,560
  • 1
  • 13
  • 14
  • 1
    Is this -- tricks works with every script ? Wondering for when I have a build error and its recommended to run --stacktrace... – FMaz008 Dec 12 '20 at 03:02
  • 12
    Could you please explain what does it mean '--' here ? – Vlad Feb 17 '21 at 10:51
  • 33
    @Arhacktector It is very common for programs to use `--` as an indicator to stop processing arguments from that point as command line options, for instance `rm -- *` would not trigger force removal even if there is a file named `-f`. So for this particular command line the `--` makes the npm command not interpret `--fix` as a command line option that it should interpret, but will instead will just pass it along "raw" to the program for the run sub-command. So the end result and objective is that the `--fix` argument is passed on to lint and not consumed by npm. – hlovdal Feb 27 '21 at 13:33
  • 1
    "npm run lint -- --fix" solved my issue of EsLint warnings & errors – Sharjeel Ali Jul 28 '21 at 07:52
  • 3
    This works for me to fix auto-fixable lint errors. Thank you :) – Lojith Vinsuka Sep 13 '21 at 07:33
76

You can add a new command in package.json.

"scripts": {
    "lint": "eslint --fix --ext .js,.jsx ."
}

And then you can run it in terminal npm run lint.

Vlad Bîcu
  • 1,128
  • 7
  • 8
43

To add a new separate script to auto-fix the linting issues for files with extensions .js and .jsx, you may add the add the script in your package.json as below:

"lint:fix": "eslint --fix --ext .js,.jsx ."

So after adding the above command the scripts section of your package.json may look like as below:

...
....
"scripts": {
  "start": "react-scripts start",
  "build": "react-scripts build",
  "test": "react-scripts test",
  "eject": "react-scripts eject",
  "lint": "eslint .",
  "lint:fix": "eslint --fix --ext .js,.jsx ."
},
....
...

To run the newly added script to auto-fix the linting issues on the command line you should run the command like as below:

npm run lint:fix

Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
  • Im getting this warning , what would be my issue ```Warning: React version not specified in eslint-plugin-react settings. See https://github.com/yannickcr/eslint-plugin-react#configuration . ``` – Hithesh kumar Dec 14 '20 at 07:01
  • @Hitheshk use this https://github.com/yannickcr/eslint-plugin-react/issues/2157#issuecomment-473680689 – Rahul Singh Bhadhoriya Jul 02 '21 at 12:26
24

Try

./node_modules/.bin/eslint --fix .
Mechanic
  • 5,015
  • 4
  • 15
  • 38
Sahilgrewalhere
  • 269
  • 2
  • 3
17

eslint auto-correct-command. It fixes all issues for me.

To check the errors first:

npx eslint .

To fix the issues for all files (auto-correct options)

npx eslint --fix .
Mahbub Alam
  • 251
  • 3
  • 2
13

This is what I use:

npx eslint --fix . 

It fixes all files.

Coder Gautam YT
  • 1,044
  • 7
  • 20