我可以隐藏还是保持沉默;npm错误&引用;使用npm运行脚本时的输出?

我可以隐藏还是保持沉默;npm错误&引用;使用npm运行脚本时的输出?,npm,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": "tap

我经常做一些任务,比如“构建”和“测试”

例如,我的
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"
  }
}
> fulfillment-service@1.0.0 build d:\code\fulfillment-service
> tsc
> 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 run build
并成功运行时,输出如下:

{
  "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"
  }
}
> fulfillment-service@1.0.0 build d:\code\fulfillment-service
> tsc
> 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 run build
并失败时,输出如下:

{
  "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"
  }
}
> fulfillment-service@1.0.0 build d:\code\fulfillment-service
> tsc
> 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运行构建--静默

这在
npm帮助
npm帮助运行
,或任何其他明显的内容中都没有记录,但是通过在internet上进行一些搜索,您可以发现它记录在
npm帮助7配置
中。您也可以使用
.npmrc
中的选项

--silent
(简称:
-s
)选项抑制:

  • 开头的两行表示正在运行的命令
  • npm错误错误
  • 如果出现错误,则创建
    npm debug.log

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

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

如果您执行
npm运行build
并且TypeScript发现错误,那么您将得到
npm错误。要抑制它们,您必须将构建脚本更改为
npm run compile--silent&&npm run minify
,并使用
npm run build--silent
运行它。npm上有一个问题:(上面的评论中也提到了)

在该问题的讨论中,一些人提到创建别名,例如,
npr
(使用gcampbell在回答中描述的--silent选项)。尽管
--silent
可以隐藏一些npm类型的问题,例如格式不正确的package.json,但目前看来这是一个合理的解决方案

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

尽管这是另一种工具,但讨论中值得探讨的另一件事是,正如其他人所指出的,
--silent
的问题是您失去了所有的输出。还有一种方法似乎适用于大多数情况:

npm run something 2>/dev/null
如果您正在运行的二进制文件中有一个碰巧写入了stderr,那么它将被抑制。但是大多数节点都会写入标准输出,所以这应该不是问题


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

如果您创建了一个自定义脚本,它返回NPM错误(即使没有错误),请添加
process.exitCode=0

在脚本末尾添加文件以避免错误。

添加文件
.npmrc
到项目中,并将文件
loglevel=silent
放在与此相关的其他帖子上(但没有足够的可信度来评论!),作为权宜之计,您可以更改npm正在原地运行的进程的退出状态,这样它就不会认为它失败了。显然,这不会阻止链接命令在其后面运行。Sh执行类似于JS的布尔运算,只需在末尾添加
| | true
,例如:

"myscript": "eslint || true"

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

关于运行脚本的输出存在一个问题。看见