Silencing errors on failures for npm run-script

前端 未结 4 1715
抹茶落季
抹茶落季 2021-02-18 15:18

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


        
4条回答
  •  南旧
    南旧 (楼主)
    2021-02-18 15:50

    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

提交回复
热议问题