使用npm运行脚本时,我可以隐藏或静音“npm ERR!”输出吗?

问题描述 投票:18回答:4

我正在使用npm run script来执行诸如“构建”和“测试”之类的任务。

例如,我的package.json如下所示:

{
  "name": "fulfillment-service",
  "version": "1.0.0",
  "description": "Endpoint for CRUD operations on fulfillment status",
  "main": "src/server.js",
  "scripts": {
    "build": "tsc",
    "test": "tape tests/*.js"
  },
  "dependencies": {},
  "devDependencies": {
    "typescript": "^1.8.10"
  }
}

当我运行npm run build并且它成功时,输出如下:

> [email protected] build d:\code\fulfillment-service
> tsc

当我运行npm run build并且它失败时,输出如下:

> [email protected] build d:\code\fulfillment-service
> tsc
src/server.ts(51,81): error TS2339: Property 'connection' does not exist on type 'IncomingMessage'.
npm ERR! Windows_NT 10.0.10586
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build"
npm ERR! node v6.2.1
npm ERR! npm  v3.9.3
npm ERR! code ELIFECYCLE
npm ERR! [email protected] build: `tsc`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the [email protected] build script 'tsc'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the fulfillment-service package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     tsc
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs fulfillment-service
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls fulfillment-service
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR!     d:\code\fulfillment-service\npm-debug.log

这会使整个控制台充满无用的信息,我必须滚动到顶部以查看失败的原因。

反正是否有隐藏/沉默在开发过程中以npm ERR!开头的线条?

npm
4个回答
23
投票

你必须使用npm run build --silent

这在npm helpnpm help run或其他任何显而易见的事情中没有记录,但是通过互联网上的一些搜索,你可以发现apparently记录了npm help 7 config。您还可以在loglevel中使用.npmrc选项。

--silent(短:-s)选项抑制:

  • 这两行以>开头,说出你正在运行什么命令。
  • npm ERR!错误。
  • 如果出现错误,请创建npm-debug.log

注意:使用npm脚本运行其他npm脚本可能需要您多次使用--silent。示例package.json

{
  . . .
  "scripts": {
    "compile": "tsc",
    "minify": "uglifyjs --some --options",
    "build": "npm run compile && npm run minify"
  }
}

如果您执行npm run build并且TypeScript发现错误,那么您将从两个脚本中获取npm ERR!。要禁止它们,您必须将构建脚本更改为npm run compile --silent && npm run minify并使用npm run build --silent运行它。


0
投票

在npm上提出了一个问题:run-scripts are too noisy while used in development #8821(也在上面的评论中提到)

在该问题的讨论中,有几个人提到创建一个别名,例如npr(利用--silent选项gcampbell在他/她的回答中描述)。虽然--silent可以隐藏一些npm类型的问题,例如格式错误的package.json,但这似乎是一个合理的解决方案。

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

另外一个可能值得研究的讨论,尽管它是另一种工具,是在yarn上描述的facebook blog post


0
投票

如果您已创建自定义脚本并返回NPM错误(即使没有错误),请在脚本末尾添加process.exitCode = 0;以避免错误。


-2
投票

正如其他人所指出的那样,--silent的问题是你失去了所有输出。对于大多数情况,还有另一种方法似乎有效:

npm run something 2>/dev/null

如果您运行的其中一个二进制文件恰好写入stderr,那么将被禁止。但大多数节点的东西写入stdout,所以它应该不是问题。

当然,这只适用于支持输出重定向的shell环境。

© www.soinside.com 2019 - 2024. All rights reserved.