26

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?

3
  • This is a question concerning the shell you're running this in rather than about npm. What shell are you using? Commented Nov 21, 2017 at 17:47
  • @Timo Sorry! I'm running in bash Commented Nov 21, 2017 at 17:48
  • 2
    @TimoStaudinger Actually, I feel this should be a question on how to do this with NPM only, meaning cross-platform. Too many NPM scripts use shell specific features for things that can be done cross-platform with the right approach. Note that the accepted answer suggests npm-run-all, which is indeed cross-platform. Commented Jul 6, 2021 at 12:38

3 Answers 3

60

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
  • -s run as sequence
  • -c continue on error
2
  • it didn't work me , this is what I have in scripts of package.json { "sample:test": "npm run lint:app:some-viewer;npm run lint:app:some-viewer-e2e"} error: missing script: lint:app:some-viewer;npm Commented Apr 30, 2020 at 9:12
  • Your problem is unrelated to this issue. Please post a new question and describe your OS and environment.
    – Allen Luce
    Commented Apr 30, 2020 at 14:52
5

You can go for

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

You can read more why that works here

1
  • it didn't work me package.json``` "sample:test": "npm run lint:app:some-viewer;npm run lint:app:some-viewer-e2e", ``` Commented Apr 30, 2020 at 9:10
3

You can also do this in bash:

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

You could then alias this as a separate script in your package.json:

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

("bar" = "build and run")

And run as:

npm run bar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.