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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47419424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:36:54  来源:igfitidea点击:

Ignore errors when running npm scripts sequentially

node.jsbashnpmcommand-linesh

提问by user3334871

Whenever I build my project to be served, I have to execute three scripts:

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

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

I wanted to run these commands sequentially as:

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

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

However, due to needing to have some JQuery in my ts files, I get an error during npm run build:localthat throws Cannot find name '$'.However, my code compiles it anyways, and that line is critical to my project, so it needs to be there (for now). That error stops the sequential execution of the script. Is there a way to ignore errors and keep executing down the chain?

但是,由于需要在我的 ts 文件中有一些 JQuery,我在npm run build:local抛出过程中遇到错误Cannot find name '$'.但是,我的代码无论如何都会编译它,并且该行对我的项目至关重要,因此它需要在那里(现在)。该错误会停止脚本的顺序执行。有没有办法忽略错误并继续执行链?

回答by Allen Luce

Give this a shot:

试一试:

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

I think of &&as meaning "only run this next command if the last one doesn't error." And ;as meaning "run this next command no matter what happens to the last one." There is also ||, which means "run the next command only if the last one errors." That's handy for things like npm run build:local || echo "The build failed!"

我认为&&这意味着“如果最后一个没有错误,则仅运行下一个命令。” 而;为意思是“运行下一个命令无论发生什么事情到最后一个。” 还有||,意思是“仅当最后一个错误时才运行下一个命令”。这对于诸如此类的事情很方便npm run build:local || echo "The build failed!"

回答by Bartosz Adamczyk

You can go for

你可以去

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

You can read more why that works here

你可以在这里阅读更多为什么它起作用