Hey everyone, I’m trying to figure out how to run my NPM scripts one after the other. Here’s what I’ve got in my package.json:
"scripts": {
"say-hi": "echo \"Hey there\" && exit 1",
"do-stuff": "start cmd.exe @cmd /k \"generate-widget && exit 1\"",
"finish-up": "start D:\\MyApp\\launcher",
"say-bye": "start cmd.exe @cmd /k \"echo \"See ya\" && exit 1\""
},
I want these to run in order, but when I use pre/post prefixes, they start one after another without waiting for the previous one to finish. Is there a way to make this work with just NPM? I know Gulp can do it, but I’m trying to stick with NPM for now. Any ideas? Thanks!
Hey Dave, I’ve been in your shoes before and I totally get the frustration. Here’s what worked for me: I used the ‘npm-run-all’ package, but with a slight twist. Instead of modifying your existing scripts, you can create a new script that runs them all in sequence. Something like this:
"scripts": {
...your existing scripts...,
"run-sequence": "npm-run-all say-hi && npm-run-all do-stuff && npm-run-all finish-up && npm-run-all say-bye"
}
This way, you keep your original scripts intact and create a new one to run them in order. Just make sure to install ‘npm-run-all’ first. Then you can simply run ‘npm run run-sequence’ and watch the magic happen. It’s been a lifesaver for me in complex projects where script order really matters. Give it a shot and let me know how it goes!
yo dave, have u tried using concurrently? its pretty neat for runnin scripts in order. install it with npm install concurrently --save-dev and update ur package.json like this:
“scripts”: {
…ur existing scripts…,
“run-all”: “concurrently -k -s first "npm run say-hi" "npm run do-stuff" "npm run finish-up" "npm run say-bye"”
}
then just run npm run run-all. works like a charm for me!
I’ve encountered this issue before, and I found a solution that works well using npm-run-all package. First, install it with ‘npm install npm-run-all --save-dev’. Then, modify your package.json scripts section like this:
"scripts": {
"say-hi": "echo \"Hey there\" && exit 0",
"do-stuff": "generate-widget && exit 0",
"finish-up": "D:\\MyApp\\launcher",
"say-bye": "echo \"See ya\" && exit 0",
"run-all": "npm-run-all -s say-hi do-stuff finish-up say-bye"
}
Now, just run ‘npm run run-all’ to execute all scripts sequentially. The -s flag ensures they run in series. Also, I changed exit 1 to exit 0 to prevent npm from stopping on non-zero exit codes. This approach keeps everything within npm and works reliably.