'Running 2 commands in npm script(nodemon && sass --watch)
I have a package.json file looks like this.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node src/app.js",
"dev": "nodemon src/app.js -e js,hbs ",
"scss": "sass --watch public/scss:public/css",
"both": "nodemon src/app.js -e js,hbs && sass --watch public/scss:public/css",
"both2" : "npm run dev && npm run scss"
},
I wonder why I cannot run these 2 commands:
"both": "nodemon src/app.js -e js,hbs && sass --watch public/scss:public/css"
by
npm run both
When I try to run it, only the first command is working.
Github repository is below, just in case you need some testing.
Solution 1:[1]
Use a package called concurrently.
npm install concurrently
Then you can make a script called both
"both": "concurrently \"nodemon src/app.js -e js,hbs\" \"sass --watch public/scss:public/css\""
See javascript - How can I run multiple npm scripts in parallel?
Solution 2:[2]
On UNIX-like systems you can just use &
instead of &&
to chain your commands. &
is used for parallel script execution whereas &&
causes a sequential execution, meaning the 2nd one starts only when the 1st has finished successfully.
cmd1 & cmd2 & ... # parallel
cmd1 && cmd2 && ... # sequential
Whereas on Windows-Systems prepending each of your commands with start
and chaining them with &&
seems to do the trick...
start cmd1 && start cmd2 && start ...
For cross-platform solutions, see f. i. either concurrently, like Namko suggests, or npm-run-all. Those solve other problems as well, that might or might not occur when using the above platform-specific solutions (problems like weired/mixed terminal-outputs, multiple terminals/windows, bad control over process/thread-termination etc.) ...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nanko Prinzhorn |
Solution 2 | Mayinx |