【问题标题】:Can I hide or silence "npm ERR!" output when using npm run script?我可以隐藏或静音“npm ERR!”吗?使用 npm 运行脚本时的输出?
【发布时间】:2021-02-10 08:15:02
【问题描述】:

我正在使用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并成功时,输出如下:

> 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! 开头的行?

【问题讨论】:

  • 存在关于运行脚本输出的问题。见npm/8821

标签: npm


【解决方案1】:

您必须使用 npm run build --silent

这在npm helpnpm help run 或其他任何明显的内容中都没有记录,但是通过在互联网上进行一些搜索,您可以发现apparently 它记录在npm help 7 config 中。您也可以在.npmrc 中使用loglevel 选项。

--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 运行它。

【讨论】:

    【解决方案2】:

    将文件.npmrc添加到项目并放入文件loglevel=silent

    【讨论】:

    • 或者可以为~/.npmrc 的用户的所有项目做同样的事情(仅供参考)
    • @juggernaut 不错的补充!
    【解决方案3】:

    与此相关的其他帖子的一个变体(但没有足够的可信度来发表评论!),作为权宜之计,您可以更改 npm 在原地运行的进程的退出状态,以便更快/方便一点,以便它不认为它失败了。显然,这不会阻止链式命令在其后运行。 sh做类似JS的布尔求值,在末尾加|| true即可,例如:

    "myscript": "eslint || true"
    

    希望这也足够明显,其他开发人员可以在他们来找你之前找到它!

    【讨论】:

      【解决方案4】:

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

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

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

      该讨论中可能值得研究的另一件事是yarn,虽然它是另一种工具,但它在facebook blog post 上进行了描述。

      【讨论】:

        【解决方案5】:

        正如其他人所指出的,--silent 的问题是您丢失了 all 输出。在大多数情况下似乎还有另一种方法:

        npm run something 2>/dev/null
        

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

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

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 2015-10-13
            • 1970-01-01
            • 1970-01-01
            • 2019-07-28
            • 1970-01-01
            • 2023-03-05
            • 1970-01-01
            • 1970-01-01
            • 2016-03-29
            相关资源
            最近更新 更多