按顺序运行 npm 脚本时忽略错误

问题描述 投票:0回答:4

每当我构建要提供服务的项目时,我都必须执行三个脚本:

npm run build:local     //calls tsc to compile ts into js files
npm run build:webpack   //invokes webpack to bundle js files into one minified file 
npm run serve           //runs my lite-server to preview files

我想按顺序运行这些命令:

npm run build:local && npm run build:webpack && npm run serve

但是,由于需要在我的 ts 文件中包含一些 JQuery,我在

npm run build:local
期间收到错误,抛出
Cannot find name '$'.
但是,我的代码无论如何都会编译它,并且该行对我的项目至关重要,因此需要将其那里(暂时)。 该错误会停止脚本的顺序执行。 有没有办法忽略错误并继续沿链执行?

node.js bash npm command-line sh
4个回答
60
投票

试一试:

npm run build:local ; npm run build:webpack && npm run serve

我认为

&&
的意思是“如果最后一个命令没有错误,则仅运行下一个命令。”
;
的意思是“无论上一个命令发生什么,都运行下一个命令。”还有
||
,意思是“仅当最后一个命令出错时才运行下一个命令”。这对于
npm run build:local || echo "The build failed!"

之类的事情很方便

如果安装了 npm-run-all

,您还可以执行以下操作
npm-run-all -s -c build:local build:webpack serve
  • -s
    按顺序运行
  • -c
    出错继续

5
投票

你可以去

npm run build:local; npm run build:webpack; npm run serve

您可以在这里

了解更多其原理

3
投票

您也可以在 bash 中执行此操作:

(npm run build:local || true) && (npm run build:webpack || true) && (npm run serve || true)

然后您可以将其别名为 package.json 中的单独脚本:

bar: (npm run build:local || true) && (npm run build:webpack || true) && (npm run serve || true)

(“bar”=“构建并运行”)

并运行为:

npm run bar

0
投票
I am also facing the same issue while running the scripts in sequence regardless of previous task's status

My original script which I want to achieve all 3 tasks-
"testChrome": "npm run deleteOutput && npx playwright test --project=chrome --grep @smoke && npm run generateReport"

here, in my pipeline I am running 'npm run testChrome'
the last task of generateReport will only executes if previous 2 are passed. 

I tried with ';' but it's giving an error. I am using Playwright framework.

Below worked for me but I need to have multiple scripts (for each browser) which is not recommended; otherwise require me to change the script every time. Like below

"chrome": "npx playwright test --project=chrome --grep @smoke"
"edge": "npx playwright test --project=edge --grep @smoke"
"safari": "npx playwright test --project=safari --grep @smoke"

"runChromeTestsWithReport": "npm-run-all -s -c deleteOutput testEdge generateReport"
"runFirefoxTestsWithReport": "npm-run-all -s -c deleteOutput testEdge generateReport"
"runSafariTestsWithReport": "npm-run-all -s -c deleteOutput testEdge generateReport"

Then finally I can run 'npm run runChromeTestsWithReport'

so I would need to create 6 scripts.

Let me know if anyone has better solution on this. Thanks
© www.soinside.com 2019 - 2024. All rights reserved.