如何在运行npm脚本时抑制输出

时间:2021-09-04 06:08:58

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.

我已经决定尝试使用npm脚本作为构建工具,到目前为止我很喜欢它。我想解决的一个问题是,当运行一个运行jshint的脚本时,当某些东西没有通过linting时,我将得到大量的“npm ERR”行。我想抑制这些,因为来自linter的输出更有意义。

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

是否有一种很好的方法来设置这个全局变量,并且有一种方法可以为每个脚本运行设置它?

2 个解决方案

#1


60  

All scripts:

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

可以通过抑制npm的输出来解决这个问题,方法有以下几种:

On each npm run invocation:

对于每个npm运行调用:

npm run --silent <your-script>

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

或在全球范围内创建.npmrc文件(此文件可以在您的项目目录或您的主文件夹中),包含以下内容:

loglevel=silent

Resources:

资源:

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

npm日志级别配置:https://docs.npmjs.com/misc/config#loglevel

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

在某些脚本(比如linting)中,我曾经遇到过一个简单的技巧,就是在这些脚本的末尾添加|| true。这将在没有任何npm配置更改的情况下工作。

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.

这将确保脚本总是以0状态退出。这使npm误以为脚本成功,从而隐藏了ERR消息。如果您想要更明确,可以添加|| exit 0,它应该会得到相同的结果。

{
  "scripts": {
    "lint": "jshint || true",
   }
}

#2


22  

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

你应该能够同时使用“安静”和“安静”选项,比如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:

您还可以将stdout/stderr发送到/dev/null,如下所示:

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

#1


60  

All scripts:

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

可以通过抑制npm的输出来解决这个问题,方法有以下几种:

On each npm run invocation:

对于每个npm运行调用:

npm run --silent <your-script>

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

或在全球范围内创建.npmrc文件(此文件可以在您的项目目录或您的主文件夹中),包含以下内容:

loglevel=silent

Resources:

资源:

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

npm日志级别配置:https://docs.npmjs.com/misc/config#loglevel

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

在某些脚本(比如linting)中,我曾经遇到过一个简单的技巧,就是在这些脚本的末尾添加|| true。这将在没有任何npm配置更改的情况下工作。

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.

这将确保脚本总是以0状态退出。这使npm误以为脚本成功,从而隐藏了ERR消息。如果您想要更明确,可以添加|| exit 0,它应该会得到相同的结果。

{
  "scripts": {
    "lint": "jshint || true",
   }
}

#2


22  

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

你应该能够同时使用“安静”和“安静”选项,比如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:

您还可以将stdout/stderr发送到/dev/null,如下所示:

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