node.js 运行 npm 脚本时如何抑制输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34426332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to suppress output when running npm scripts
提问by Sanketh Katta
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?
有没有一种全局设置的好方法,有没有一种方法可以为每个脚本运行设置它?
回答by Sanketh Katta
All scripts:
所有脚本:
You can fix this by suppressing the output of npm overall, by setting the log level to silentin a couple ways:
您可以通过silent以下几种方式将日志级别设置为整体抑制 npm 的输出来解决此问题:
On each npm runinvocation:
在每次npm run调用时:
npm run --silent <your-script>
Or globally by creating a .npmrcfile(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 || trueat 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 0status. This tricks npm into thinking the script succeed, hence hiding the ERRmessages. If you want to be more explicit, you can append || exit 0instead and it should achieve the same result.
这将确保脚本始终以0状态退出。这会让 npm 认为脚本成功了,从而隐藏了ERR消息。如果你想更明确,你可以|| exit 0改为附加,它应该达到相同的结果。
{
"scripts": {
"lint": "jshint || true",
}
}
回答by Alexander Mills
You should be able to use both the --quietand --silentoptions, as in
您应该能够同时使用--quiet和--silent选项,如
npm install --quiet
--quietwill show stderr and warnings, --silentshould suppress nearly everything
--quiet将显示 stderr 和警告,--silent应该抑制几乎所有内容
You can also send stdout/stderr to /dev/null, like so:
您还可以将 stdout/stderr 发送到/dev/null,如下所示:
npm install > "/dev/null" 2>&1
or less versbose
或不那么冗长
npm install &> /dev/null
回答by Michael Cole
npm install --quiet --no-progress
Will keep warnings and errors, and suppress the ADHD progress bar on terminals that support it.
将保留警告和错误,并在支持它的终端上抑制 ADHD 进度条。
回答by ErisDS
You can do this inside your script by removing the event listeners
您可以通过删除事件侦听器在脚本中执行此操作
#!/usr/bin/env node
process.removeAllListeners('warning');
// Do your thang without triggering warnings

