【问题标题】:Catch npm ERR! when running npm test from shell script捕捉 npm 错误!从 shell 脚本运行 npm test 时
【发布时间】:2015-06-29 19:53:17
【问题描述】:

我有一个 shell 脚本,它从基本目录递归搜索 package.json 并像这样运行 npm test。

find "$parent_dir" -name package.json -maxdepth 2 -execdir npm test \;

我想拦截测试失败的形式:

npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

所以我可以让 shell 脚本以错误状态退出。

感谢任何帮助。

【问题讨论】:

标签: node.js shell npm


【解决方案1】:

又晚了 3 年,也在寻找解决方案。

终于找到了,如果这些对将来的其他人有帮助。我使用了 shell 脚本 $? command,返回最后执行的命令的退出状态。 IE。如果 shell 脚本失败,则返回 1,如果成功则返回 0。因此 NPM 实现了这个返回。

npm run test
if [ $? -eq 0 ]
then
  echo "SUCCESS"
else
  echo "FAIL"
fi

【讨论】:

【解决方案2】:

如果我理解正确,如果 npm ERR,您想执行某些操作或停止执行脚本!被扔了? 这可能会有所帮助:

 if ./somecommand | grep -q 'npm ERR!'; then
      #what you want to do
      # exit executing the script
      return
    fi

对不起,我迟到了两年,刚加入 SO 社区。​​p>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 2018-11-16
    • 2017-09-09
    • 1970-01-01
    • 2015-11-05
    相关资源
    最近更新 更多