Multiple commands in package.json

node.jspackage.json

node.js Problem Overview


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)

node.js Solutions


Solution 1 - node.js

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

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

Solution 2 - node.js

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"
}

Solution 3 - node.js

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"
}

Solution 4 - node.js

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"
}

Solution 5 - node.js

Better understand the && operator

In my case the && didn't worked well because I had a package command that was killing previous instance of my server and returned 1 (error) if the server didn't already exists and 0 (success) if it was existing AND the && chaining operator works only if first command succeeds.

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

  • && run second command after and only if first succeeds
  • || run second command after and 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 second command after whatever the first command outputted
  • ; WIN separate command arguments
  • & UNIX run first command in the background parallel to the second one
  • & WIN run second command after whatever the first command outputted

All chaining operators for windows here and for unix here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRedmontyView Question on Stackoverflow
Solution 1 - node.jsVladyslav MoisieienkovView Answer on Stackoverflow
Solution 2 - node.jspdoherty926View Answer on Stackoverflow
Solution 3 - node.jsNabin Kumar KhatiwadaView Answer on Stackoverflow
Solution 4 - node.jsWebDeverView Answer on Stackoverflow
Solution 5 - node.jsTOPKATView Answer on Stackoverflow