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


我在用着 npm运行脚本 做“构建”和“测试”等任务。

例如,我的 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 它成功了,输出如下:

> fulfillment-service@1.0.0 build d:\code\fulfillment-service
> tsc

当我跑 npm run build 它失败了,输出如下:

> fulfillment-service@1.0.0 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! fulfillment-service@1.0.0 build: `tsc`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the fulfillment-service@1.0.0 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! 在开发期间?


12004
2018-06-28 18:52


起源

npm run build --silent - gcampbell
关于运行脚本的输出存在一个问题。看到 NPM / 8821 - styfle


答案:


你必须使用 npm run build --silent

这没有记录在案 npm helpnpm help run,或其他任何明显的,但在互联网上搜索一些你可以找到 显然地 它被记录在案中 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


14
2017-07-04 08:15





在npm上提出了一个问题: 在开发#8821中使用run-scripts太嘈杂 (也在上面的评论中提到)

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

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

这个讨论的另一件事可能值得研究,虽然它是另一种工具,但是  在a上描述 facebook博客文章


0
2018-04-28 22:29





正如其他人所说,问题所在 --silent 你输了吗? 所有 输出。对于大多数情况,还有另一种方法似乎有效:

npm run something 2>/dev/null

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

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


-1
2017-11-07 02:05