72

This command: "start": "node server/server.js" starts my server, but before running this I also want a command to run automatically: 'webpack'.

I want to build a script that can be run with npm run someCommand - it should first run webpack in the terminal, followed by node server/server.js.

(I know how configure this with gulp, but I don't want to use it)

CSJ
  • 3,391
  • 2
  • 18
  • 27
Redmonty
  • 823
  • 1
  • 6
  • 4

5 Answers5

130

If I understood you correctly, you want firstly run webpack and after compile run nodejs. Maybe try this:

"start": "webpack && node server/server.js"
Vladyslav Moisieienkov
  • 3,430
  • 3
  • 19
  • 30
  • And how to pass args to both inner commands? For example I want to run "npm run start -- --dest dest/env" and I want dest arg for webpack and node command. – Alexey Bychkov Aug 07 '20 at 10:33
39

The following should work:

"start": "webpack && node server/server.js"

Though, for readability (and especially if you plan on adding additional tasks in the future), you may want to consider creating separate entries for each task and then calling each of those from start. Something like:

{
    "init-assets": "webpack",
    "init-server": "node server/server.js",
    "start": "npm run init-assets && npm run init-server"
}
Teocci
  • 5,312
  • 1
  • 41
  • 41
pdoherty926
  • 10,116
  • 2
  • 34
  • 61
11

You can also chain commands like this:

"scripts": {
    "clean": "npm cache clean --force",
    "clean:complete": "npm run clean && npm uninstall -g @angular/cli && rmdir /Q /S node_modules",
    "clean:complete:install": "npm run clean:complete && npm i -g @angular/cli && npm i && npm install --save-dev @angular/cli@latest"
}
Teocci
  • 5,312
  • 1
  • 41
  • 41
Nabin Kumar Khatiwada
  • 1,426
  • 15
  • 19
4

Also, along with the accepted answer and @pdoherty926's answer, in case you want to have run two command prompts, you can add "start" before each command:

{
    "init-assets": "webpack",
    "init-server": "node server/server.js",
    "start": "start npm run init-assets && start npm run init-server"
}
DrakaSAN
  • 7,323
  • 7
  • 47
  • 89
WebDever
  • 5,154
  • 2
  • 13
  • 13
4

Better understand the && operator

In my case the && didn't worked well because one of my commands exited with 1 (error) or 0 (success) depending on the situation AND the && chaining operator works only if the previous command succeeds.

So, to add to other answers here are the options you get to separate commands:

  • && run second command only if first succeeds
  • || run second command only if first fails

So if you want the second command to run whatever the first has outputted the best way is to do something like (command1 && command2) || command 2

OS specific chaining operators

Other options are different in unix (linux, macos) and windows environnement

  • ; UNIX run the second command whatever the first has outputted
  • ; WIN separate command arguments
  • & UNIX run first command in the background parallel to the second one
  • & WIN run the second command whatever the first has outputted

All chaining operators for windows here and for unix here

TOPKAT
  • 4,513
  • 2
  • 27
  • 55