Can I hide or silence "npm ERR!" output when using npm run script?

I'm using npm run script to do tasks such as "build" and "test".

For example, my package.json looks like the following:

{
  "name": "fulfillment-service",
  "version": "1.0.0",
  "description": "Endpoint for CRUD operations on fulfillment status",
  "main": "src/server.js",
  "scripts": {
    "build": "tsc",
    "test": "tape tests/*.js"
  },
  "dependencies": {},
  "devDependencies": {
    "typescript": "^1.8.10"
  }
}

When I run npm run build and it is successful, the output is the following:

> [email protected] build d:\code\fulfillment-service
> tsc

When I run npm run build and it fails, the output is the following:

> [email protected] build d:\code\fulfillment-service
> tsc
src/server.ts(51,81): error TS2339: Property 'connection' does not exist on type 'IncomingMessage'.
npm ERR! Windows_NT 10.0.10586
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build"
npm ERR! node v6.2.1
npm ERR! npm  v3.9.3
npm ERR! code ELIFECYCLE
npm ERR! [email protected] build: `tsc`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the [email protected] build script 'tsc'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the fulfillment-service package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     tsc
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs fulfillment-service
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls fulfillment-service
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR!     d:\code\fulfillment-service\npm-debug.log

This fills the entire console with useless information, and I have to scroll to the top to see why it failed.

Is there anyway to hide/silence the lines that start with npm ERR! during development?


Solution 1:

You have to use npm run build --silent.

This isn't documented in npm help, npm help run, or anything else obvious, but with some searching on the internet you can find out that apparently it is documented in npm help 7 config. You can also use the loglevel option in .npmrc.

The --silent (short: -s) option suppresses:

  • The two lines beginning with > that say what command you're running.
  • npm ERR! errors.
  • Creating an npm-debug.log if there's an error.

Note: using npm scripts to run other npm scripts may require you to use --silent more than once. Example package.json:

{
  . . .
  "scripts": {
    "compile": "tsc",
    "minify": "uglifyjs --some --options",
    "build": "npm run compile && npm run minify"
  }
}

If you do npm run build and TypeScript finds an error, then you'll get the npm ERR! from both scripts. To suppress them, you have to change the build script to npm run compile --silent && npm run minify and run it with npm run build --silent.

Solution 2:

Add file .npmrc to project and put in the file loglevel=silent.

Solution 3:

A variation on other posts related to this (but not enough cred to comment!), and a little quicker/convenient as a stop gap is that you can change the exit status of processes that npm is running in situ, so that it doesn't think it failed. Obviously this will not stop chained commands from running after it. Sh does boolean evaluation similar to JS, just add || true on the end, e.g.:

"myscript": "eslint || true"

Hopefully this is also obvious enough that other devs can find it before they come looking for you!