为了账号安全,请及时绑定邮箱和手机立即绑定

如何继续运行在 package.json 中定义的脚本,以防万一失败?

如何继续运行在 package.json 中定义的脚本,以防万一失败?

浮云间 2022-09-29 16:32:30
我在包.json文件中有这个:{ "name": "test demo", "version": "1.0.0", "description": "test demo scripts", "scripts": {   "test:v1": "wdio src/resources/config/wdio.chrome.conf.js --spec test/Tests.spec.ts",   "test:v2": "wdio src/resources/config/wdio.firefox.conf.js --spec test/Tests.spec.ts",   "test": "npm run test:v1 && npm run test:v2"   },    ...}运行此命令时:npm run test然后,如果失败,则根本不执行脚本。有没有办法对此进行配置,以便所有脚本都运行,而不管其中一些脚本是否失败?test:v1test:v2编辑:当我在包.json中尝试这个时:"test": "npm run test:v1 ; npm run test:v2"然后我仍然没有执行两个脚本,只是执行错误。这是我在控制台中得到的:test:v1C:\2020\demo>npm run test> demo@1.0.0 test C:\2020\demo> npm run test:v1 ; npm run test:v2> demo@1.0.0 test:v1 C:\2020\demo> wdio src/resources/config/wdio.chrome.conf.js --spec test/Tests.spec.ts ";"   "npm" "run" "test:v2"Execution of 1 spec files started at 2020-06-12T15:16:42.936Z[0-0] RUNNING in chrome - C:\2020\demo\Tests.spec.ts... other log with failing tests ...Spec Files:      0 passed, 1 failed, 1 total (100% completed) in 00:00:27npm ERR! code ELIFECYCLEnpm ERR! errno 1npm ERR! demo@1.0.0 test:v1: `wdio src/resources/config/wdio.chrome.conf.js --spec test/Tests.spec.ts ";" "npm" "run" "test:v2"`npm ERR! Exit status 1npm ERR!npm ERR! Failed at the demo@1.0.0 test:v1 script.npm ERR! This is probably not a problem with npm. There is likely additional logging output above.npm ERR! A complete log of this run can be found in:npm ERR!     C:\Users\<User>\AppData\Roaming\npm-cache\_logs\2020-06- 12T15_17_10_512Z-debug.lognpm ERR! code ELIFECYCLEnpm ERR! errno 1npm ERR! demo@1.0.0 test: `npm run test:v1 ; npm run test:v2`npm ERR! Exit status 1npm ERR!npm ERR! Failed at the demo@1.0.0 test script.npm ERR! This is probably not a problem with npm. There is likely additional logging output above.npm ERR! A complete log of this run can be found in:npm ERR!     C:\Users\<User>\AppData\Roaming\npm-cache\_logs\2020-06- 12T15_17_10_548Z-debug.log如果我在包.json中尝试这样:"test": "npm run test:v1; npm run test:v2"
查看完整描述

2 回答

?
慕侠2389804

TA贡献1719条经验 获得超6个赞

此问题的解决方案因您使用的操作系统而异,因为 NPM 使用不同的 shell 来运行 npm 脚本。

  • 在 *nix(Linux、macOS 等)上,npm 用于运行 npm 脚本的默认外壳程序是 。sh

  • 在视窗上,npm 用于运行 npm 脚本的默认外壳程序是 。cmd.exe


检查 npm 正在使用哪个外壳?

使用 npm 配置命令,您需要检查脚本外壳程序设置的值。为此,您可以运行以下命令:

npm config get script-shell

它通常会返回/打印路径名到 或 - 具体取决于您使用的操作系统。cmd.exesh


溶液:

  1. 如果你运行的是视窗,并且默认外壳程序为 cmd.exe

    然后将包.json 中的脚本更改为以下内容:test

    "test": "npm run test:v1 & npm run test:v2"

    请注意,双 & 符号 () 已被替换为单 & 符号 ()。&&&

或。。。

  1. 如果你运行的是 *nix,并且默认外壳是 sh

    然后将包.json 中的脚本更改为以下内容:test

    "test": "npm run test:v1; npm run test:v2"

    请注意,双 & 符号 () 已替换为单个分号 ()。&&;


查看完整回答
反对 回复 2022-09-29
?
PIPIONE

TA贡献1829条经验 获得超9个赞

npm run test:v1; npm run test:v2

;表示运行下一个命令,而不管前一个命令是否成功。

&&意味着仅当上一个命令成功时才运行下一个命令。


查看完整回答
反对 回复 2022-09-29
  • 2 回答
  • 0 关注
  • 111 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信