Npm scripts ignore errors

I currently have an npm script to run linter. Obviously, some errors will be displayed, but the npm script crashes, and not just shows me errors and moves on.

This is terrible, especially when I have something else called a script, as it miraculously breaks everything. I can always run npm run lint --force to avoid errors, but I don't have this --force luxury all the time (e.g. with git-hook).

How can I configure my script to output errors without causing a mess?

+6
source share
3 answers

Found answer:

Just adding: exit 0 to the end of the command did it!

+3
source

There are several more options that are also cross-platform:

eslint || true

To work on Windows you need npm install cash-true first

exitzero eslint

You need npm install exitzero first

+3
source

I think the solution that in npm scripts will always be a bit limited. There is a micro-module runjs , which is a kind of small improvement for scripts. The fact is that you can run cli commands and handle errors in JS. So you can do something like this ( runfile.js ):

 import { run } from 'runjs' export function lint () { try { run('eslint .') } catch (e) { // handle error how you want, you can silently just do nothing or re-throw it by: throw e.stack } } 

then in the console:

 $ run lint 
0
source

Source: https://habr.com/ru/post/987550/


All Articles