1

I want to run multiple npm scripts synchronous.(one after another) For example here are my npm scripts.I have both bower and npm packages in my project.

{
  "scripts": {
    "installnpm": "npm i",
    "installbower": "bower i",
    "rimraf":"rimraf dist"
    "lernabootstrap": "lerna bootstrap",
    "start":"nodemon myApp.js"
  }
} 

How can I run multiple scripts both on unix and windows without any error? Scripts also includes npm and bower install commands like you see.Does it matter for this situation?

javauser35
  • 1,177
  • 2
  • 14
  • 34
  • 1
    When you say `synchronously`, I assume by that you mean serially, as in one after another? That's generally what "synchronous" means when describing code. If so, I believe both *nix and windows support the `&&` operator between commands. e.g. `npm i && bower i && ...` – David784 Feb 08 '20 at 15:46
  • @David784 I haven't seen the syntax of && on Windows as a thing. Is that something npm implements specifically or do you have a reference that that's viable? – Joe Feb 08 '20 at 15:55
  • @Joe, why don't you just [try it!!!?!?!?!](https://pastebin.com/0SNc2bRS) – gman Feb 08 '20 at 16:17
  • @gman I don't have a windows system handy – Joe Feb 08 '20 at 16:33
  • I just tried it in my win10 VM, and it works great. Also [Here's a historical SO answer](https://stackoverflow.com/questions/8055371/how-do-i-run-two-commands-in-one-line-in-windows-cmd) that gives some history. Here's the example I used: `dir && echo "hello world"` – David784 Feb 08 '20 at 16:36

1 Answers1

4

You can run commands synchronously by using && in both Windows and Unix environments.

So ...

{
  "scripts": {
    "installnpm": "npm i",
    "installbower": "bower i",
    "rimraf":"rimraf dist"
    "lernabootstrap": "lerna bootstrap",
    "start":"nodemon myApp.js",
    "all": "npm run installnpm && npm run installbower && npm run start"
  }
} 

so then you could run npm run all to execute all those npm scripts synchronously

Here is a answer that answers it for Windows and this answer is useful as well.

james
  • 5,006
  • 8
  • 39
  • 64