顺序运行npm脚本时忽略错误 [英] Ignore errors when running npm scripts sequentially

查看:531
本文介绍了顺序运行npm脚本时忽略错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是,由于需要在ts文件中包含一些JQuery,因此出现错误在 npm运行build:local 时抛出找不到名称'$'。但是,我的代码还是会编译它,并且该行对我的项目至关重要,因此(现在)它必须存在。该错误将停止脚本的顺序执行。有没有办法忽略错误并继续执行下去?

However, due to needing to have some JQuery in my ts files, I get an error during npm run build:local that 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?

推荐答案

试一下:

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

我认为&& 的含义如果最后一个没有错误,则仅运行下一个命令。而; 的意思是不管最后一个发生什么,都运行此下一个命令。还有 || ,这意味着仅在最后一个错误时才运行下一个命令。这对于诸如 npm run build:local ||之类的事情很方便回显构建失败!

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-all

npm-run-all -s -c build:local build:webpack serve



  • -s 按顺序运行

  • -c 继续错误出现

    • -s run as sequence
    • -c continue on error
    • 这篇关于顺序运行npm脚本时忽略错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆