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

后端 未结 5 1400
余生分开走
余生分开走 2021-01-03 18:04

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

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

{
  \"name\         


        
相关标签:
5条回答
  • 2021-01-03 18:16

    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.

    0 讨论(0)
  • 2021-01-03 18:19

    There is an issue filed on npm: run-scripts are too noisy while used in development #8821 (also mentioned in a comment above)

    In the discussion in that issue, a couple of people have mentioned creating an alias e.g. npr (utilizing the --silent option gcampbell describes in his/her answer). Although --silent can hide some npm type issues such as a malformed package.json, this seems like a reasonable solution for now.

    alias npr='npm run --silent $*'
    

    One other thing from that discussion which may be worth looking into, although it is yet another tool, is yarn which is described on a facebook blog post.

    0 讨论(0)
  • 2021-01-03 18:23

    As others have noted, the problem with --silent is you lose all output. There's another way that seems to work for most cases:

    npm run something 2>/dev/null
    

    If one of the binaries you are running happens to write to stderr, then that will be suppressed. But most node stuff writes to stdout, so it shouldn't be a problem.

    Of course this will work only in a shell environment that supports output redirection.

    0 讨论(0)
  • 2021-01-03 18:31

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

    0 讨论(0)
  • 2021-01-03 18:39

    In case if you've created a custom script and it returns with the NPM error (even there's no error), add process.exitCode = 0; at the end of the script to avoid error.

    0 讨论(0)
提交回复
热议问题