Javascript 如果失败,如何继续运行package.json中定义的脚本?
我在package.json文件中有以下内容:Javascript 如果失败,如何继续运行package.json中定义的脚本?,javascript,node.js,package.json,webdriver-io,npm-scripts,Javascript,Node.js,Package.json,Webdriver Io,Npm Scripts,我在package.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/
{
"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:v1
失败,则根本不执行test:v2
脚本。是否有一种方法可以配置这些脚本,使所有脚本都可以运行,而不管其中一些脚本是否失败 编辑: 在package.json中尝试此操作时:
"test": "npm run test:v1 ; npm run test:v2"
"test": "npm run test:v1; npm run test:v2"
然后我仍然没有执行这两个脚本,只是执行了带有错误的test:v1
。这是我在控制台中得到的:
C:\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:27
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm 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 1
npm 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.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! demo@1.0.0 test: `npm run test:v1 ; npm run test:v2`
npm ERR! Exit status 1
npm 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
npm ERR! missing script: test:v1;
npm ERR!
npm ERR! Did you mean one of these?
npm ERR! test:v1
npm ERR! test:v2
然后我在控制台中得到这个:
C:\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:27
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm 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 1
npm 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.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! demo@1.0.0 test: `npm run test:v1 ; npm run test:v2`
npm ERR! Exit status 1
npm 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
npm ERR! missing script: test:v1;
npm ERR!
npm ERR! Did you mean one of these?
npm ERR! test:v1
npm ERR! test:v2
谢谢
npm run test:v1; npm run test:v2
表示运行下一个命令,而不管上一个命令是否成功
&&
意味着只有在前一个命令成功的情况下才运行下一个命令。此解决方案因您使用的操作系统而异,因为NPM使用不同的shell来运行NPM脚本
- 在*nix(Linux、macOS等)上,npm用于运行npm脚本的默认shell是
sh
- 在Windows上,npm用于运行npm脚本的默认shell是
cmd.exe
检查哪个shell npm正在使用? 使用该命令,您需要检查设置的值。为此,可以运行以下命令:
npm配置获取脚本shell
它通常会将路径名返回/打印到cmd.exe
或sh
——具体取决于您使用的操作系统
解决方案:
cmd.exe
然后将package.json中的test
脚本更改为以下内容:
"test": "npm run test:v1 & npm run test:v2"
"test": "npm run test:v1; npm run test:v2"
请注意,双符号(&&
)已被单符号(&
)替换
sh
然后将package.json中的test
脚本更改为以下内容:
"test": "npm run test:v1 & npm run test:v2"
"test": "npm run test:v1; npm run test:v2"
请注意,双符号(&&
)已替换为一个分号(;
)
谢谢你的回复!根据你的评论,我用我尝试过的方法编辑了我的问题。不幸的是,它没有起作用。仅执行了第一个脚本,失败后另一个脚本未启动。您可以使用。同时运行
npm install
进行安装,然后在您的package.json脚本中执行“测试”:“同时”npm Run test:v1\“\”npm Run test:v2\”
。这将基本上同时运行两个测试命令。如果运行npm config get script shell
它报告哪个shell?它是否报告:1)sh
-这是npm在*nix平台上运行npm脚本时使用的默认shell。2) 或者它是否报告cmd.exe
——这是npm在Windows平台上运行npm脚本时使用的默认shell。如果是后者,即如果是cmd.exe
,则更改test
脚本以使用单个符号,例如:“test”:“npm run test:v1&npm run test:v2”
@RobC感谢您的评论!这就是我得到的:C:\Program Files\nodejs>npm config get script shell null C:\Program Files\nodejs>npm config get shell C:\WINDOWS\system32\cmd.exe
是的,所以您使用的是WINDOWS,npm脚本使用的默认shell是cmd.exe
-这就是为什么您尝试使用分号(;
)-根据@gzcz答案,它不起作用。分号(;
)可以满足您在*nix平台(Linux、macOS等)上的要求,因为npm使用sh
作为npm脚本的shell。根据我的第一条评论,您需要尝试使用。例如:“测试”:“npm运行测试:v1&npm运行测试:v2”
@RobC非常感谢你!这有帮助。它与和一起工作。能否请您添加一条评论作为答案,以便我可以接受:)