运行 npm 脚本时如何抑制输出 [英] How to suppress output when running npm scripts
问题描述
我决定尝试使用 npm 脚本作为构建工具,到目前为止我喜欢它.我想解决的一个问题是,在运行脚本以运行 jshint 时,如果某些内容未通过 linting,我会收到大量npm ERR!"线.我想抑制这些,因为 linter 的输出更有意义.
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.
是否有一种全局设置的好方法,以及是否可以为每个脚本运行设置它?
Is there a good way to set this globally and is there a way to set it for each script run?
推荐答案
所有脚本:
您可以通过以下几种方式将日志级别设置为 silent
来整体抑制 npm 的输出来解决此问题:
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 run
调用上:
npm run --silent <your-script>
或者通过创建一个 .npmrc
文件(这个文件可以在你的项目目录或你的主文件夹中)来全局使用以下内容:
Or globally by creating a .npmrc
file(this file can be either in your project directory or your home folder) with the following:
loglevel=silent
资源:
npm 日志级别配置:https://docs.npmjs.com/misc/config#loglevel
npm log level config: https://docs.npmjs.com/misc/config#loglevel
npmrc:https://docs.npmjs.com/misc/config#loglevel
我用来在某些脚本(如 linting)上解决此问题的一个简单技巧是附加 ||在此类脚本的末尾为 true
.这将在没有任何 npm 配置更改的情况下工作.
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.
这将确保脚本始终以 0
状态退出.这会让 npm 认为脚本成功了,从而隐藏了 ERR
消息.如果你想更明确,你可以附加 ||exit 0
代替,它应该达到相同的结果.
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.
{
"scripts": {
"lint": "jshint || true",
}
}
这篇关于运行 npm 脚本时如何抑制输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!