Silencing errors on failures for npm run-script

2019-04-18 07:38发布

When you run npm test and it fails, you get the test outputs + a single error message, like so:

npm ERR! Test failed.  See above for more details.

However, I made a custom script called lint, like so:

// package.json
{
  // ...
  "scripts": {
    // ... definition for test ...
    "lint": "./node_modules/jsxhint/cli.js src/",
  }
}

Alright, simple enough. But when you run npm run lint and it fails, Rather than the nice looking error for npm test, you get a massive error message after the output of the linter:

npm ERR! Darwin 14.0.0
npm ERR! argv "node" "/usr/local/bin/npm" "run-script" "lint"
npm ERR! node v0.10.32
npm ERR! npm  v2.1.7
npm ERR! code ELIFECYCLE
# and ~15 more lines...

Is there a way to silence all this junk so I can have a clean output like the npm test script? I see how they caught the error in the npm source code, but I don't think I can just add a custom command without forking npm like that... Hope I'm wrong!

But if I am, would I be better off just pushing off a task like this to a tool like Grunt? Thanks!

标签: node.js npm
4条回答
走好不送
2楼-- · 2019-04-18 07:55

Use the npm run --silent option:

$ npm run --silent test

Even less typing if you define a shell alias:

$ alias run='npm run --silent'
$ run test
查看更多
Root(大扎)
3楼-- · 2019-04-18 08:00

If you don't care about preserving the return code of the linter process, you can always configure your package.json like this:

{
  // ...
  "scripts": {
    // ...
    "lint": "eslint . || true",
  }
}
查看更多
做个烂人
4楼-- · 2019-04-18 08:05

You can silence the errors by redirecting the stderr to /dev/null. For example:

{
 "test": "karma start" (package.json)
}

running:

$ npm run test 2> /dev/null

will now send all npm errors to /dev/null but normal input will still be visible in the console.

Because the error is thrown by npm, after karma exiting with a non-zero status, doing the following is not enough:

 {
     "test": "karma start 2> /dev/null"
 }

but you can overcome it by creating another task that calls that task with stderr redirection:

 {
     "test": "karma start",
     "test:silent": "npm run test 2> /dev/null" 
 }

this will ensure that the npm error messages are hidden

查看更多
欢心
5楼-- · 2019-04-18 08:18

I've just been trying to figure out the same. Not a perfect answer but it kind of worked to specify linting as a pretest script (docs) like so:

// package.json
{
  // ...
  "scripts": {
    // ... definition for test ...
    "pretest": "./node_modules/jsxhint/cli.js src/",
  }
}

Then, when you type in npm test the first time, you will only get a single-line error from NPM. Obviously, that means you won't be able to run your tests if you haven't linted.

The other option is to use some kind of third party task runner like Make, Grunt or Gulp.

I've only used Make, and I think it's the most painless to set up (at least on OSX and Linux, not sure about Windows).

Create a Makefile in your root that looks like so:

lint:
    ./node_modules/.bin/jslint ./*.js # or whatever your lint command is

test:
    ./node_modules/.bin/mocha test/*.js # or whatever your test command is

.PHONY: lint test

Then type make test and make lint to run those commands.

查看更多
登录 后发表回答