151

I have decided to experiment with npm scripts as a build tool and so far I like it. One issue I'd like to solve is when running a script to run jshint when something doesn't pass linting I get a ton of "npm ERR!" lines. I would like to suppress these as the output from the linter is more meaningful.

Is there a good way to set this globally and is there a way to set it for each script run?

2

5 Answers 5

242

All scripts:

You can fix this by suppressing the output of npm overall, by setting the log level to silent in a couple ways:

On each npm run invocation:

npm run --silent <your-script>

Or by creating a .npmrc file (this file can be either in your project directory -local- or your home folder -global-) with the following:

loglevel=silent

Resources:

npm log level config: https://docs.npmjs.com/misc/config#loglevel

npmrc: https://docs.npmjs.com/misc/config#loglevel

Each script, individually:

A simple trick I've used to get around this issue on certain scripts like linting is to append || true at the end of such scripts. This will work without any npm config changes.

This will ensure that the script will always exit with a 0 status. This tricks npm into thinking the script succeed, hence hiding the ERR messages. If you want to be more explicit, you can append || exit 0 instead and it should achieve the same result.

{
  "scripts": {
    "lint": "jshint || true",
   }
}
5
  • 1
    || true doesnt work if you are trying to append args onto the end of the npm run - eg. npm run myCmd -- --deploy
    – arcseldon
    Feb 7, 2016 at 10:05
  • 11
    This is really quite absurd (not blaming you). I don't want to append || true; that is not a good solution. I don't want to silent ALL other commands using .npmrc. And running this particular script with -s all the time also seems very silly. Did anyone find a better solution for silencing a single script? Jul 28, 2016 at 8:39
  • In case anyone else comes across this, there's is an open issue - see github.com/npm/npm/issues/8821. Oct 4, 2016 at 8:00
  • loglevel=silent seems overkill to me. That would silence even error messages (though they would still be written to a local file). According to the link soon after this suggestion, the possible log levels, in priority order, are: "silent", "error", "warn", "notice", "http", "timing", "info", "verbose", "silly". I would suggest either "error" (which would suppress warnings, but display errors) or "warn" (which would include warnings). There is normally no reason for npm to show us how it decides what to run, which is what it does by default. That feels like debugging text. Sep 28, 2018 at 0:47
  • OK, I need to correct myself, and raise an objection. Maybe it's even a node.js bug? When I created a .npmrc file with 'loglevel=error', running 'npm test' still traced through npm's logic about how it determined what command line to run. Node version 8.12.0, npm version 5.8.0. Sep 28, 2018 at 0:55
70

You should be able to use both the --quiet and --silent options, as in

npm install --quiet

--quiet will show stderr and warnings, --silent should suppress nearly everything

You can also send stdout/stderr to /dev/null, like so:

npm install > '/dev/null' 2>&1

or less versbose

npm install &> '/dev/null'
3
  • 8
    In Windows, it is npm install --quiet > NUL Apr 30, 2017 at 10:14
  • 6
    This question is specifically about running npm scripts.
    – hackel
    Oct 20, 2019 at 22:41
  • --quiet works for npm scripts ;) @hackel Dec 5, 2020 at 15:33
24
npm install --quiet --no-progress 

Will keep warnings and errors, and suppress the ADHD progress bar on terminals that support it.

3
  • 6
    This question is specifically about running npm scripts.
    – hackel
    Oct 20, 2019 at 22:41
  • 2
    --quiet does not seem to work for me at all. --silent works, but annoying progress bar still shows. --no-progress does not hide progress bar. That's for npm 7.21.1.
    – vbezhenar
    Oct 6, 2021 at 5:48
  • The question is about running scripts, but it's the top google hit for "npm install quietly no warning" Aug 9, 2022 at 4:13
5

for an individual script that you want to keep silent without having to add --silent each time, you can make a new script that calls your previous one and adds --silent.

My example scripts in package.json:

    "dev-loud": "npm run build && nodemon -r dotenv/config dist/index.js",
    "dev": "npm run dev-loud --silent"
2
  • I think that when something goes wrong for (following your example) "dev-loud" script the "--silent" arg will suppress the logs but the initial executed line "npm run dev" will still throw an exception
    – lmoglia
    Mar 17, 2021 at 17:33
  • I like this approach for adding hacky help messages, like "release:message": "echo '\n some long \n multiline message'", which would otherwise be double printed 👍
    – ptim
    Dec 23, 2021 at 1:05
3

You can do this inside your script by removing the event listeners

#!/usr/bin/env node

process.removeAllListeners('warning');

// Do your thang without triggering warnings

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.