161

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.
2
  • can you share error message/ stack trace?
    – Gyandeep
    Oct 26, 2016 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. Oct 26, 2016 at 22:06

6 Answers 6

284

You can run below command:

npm run lint -- --fix
5
  • 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, 2020 at 3:02
  • 12
    Could you please explain what does it mean '--' here ?
    – Vlad
    Feb 17, 2021 at 10:51
  • 30
    @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, 2021 at 13:33
  • 1
    "npm run lint -- --fix" solved my issue of EsLint warnings & errors Jul 28, 2021 at 7:52
  • 3
    This works for me to fix auto-fixable lint errors. Thank you :) Sep 13, 2021 at 7:33
71

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.

2
  • what does --ext stand for? Feb 25, 2022 at 16:41
  • 1
    --ext is the flag for extension
    – Vlad Bîcu
    Feb 26, 2022 at 17:03
40

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

2
22

Try

./node_modules/.bin/eslint --fix .
3
13

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 .
12

This is what I use:

npx eslint --fix . 

It fixes all files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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