Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple commands in package.json

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)

like image 393
Redmonty Avatar asked Oct 18 '17 13:10

Redmonty


People also ask

How do I combine two npm scripts?

&& will run your scripts sequentially while & will run them in parallel. A quick way of doing it is npm run start-watch & npm run wp-server . This will run the first command as a background thread. This works really well when one of the commands is not long running and does not need to be manually exited later.


2 Answers

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

"start": "webpack && node server/server.js" 
like image 149
Vladyslav Moisieienkov Avatar answered Oct 06 '22 12:10

Vladyslav Moisieienkov


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" } 
like image 45
pdoherty926 Avatar answered Oct 06 '22 11:10

pdoherty926