node.jsbashnpmcommand-linesh

Ignore errors when running npm scripts sequentially


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: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?


Solution

  • 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!"

    You can also do the following if you install npm-run-all

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