Can I run two ongoing npm commands in 1 terminal - node.js

I have these 2 commands in my npm scripts
"scripts": {
"webpack": "webpack --config webpack.config.js --watch",
"server": "nodemon server.js",
}
As you can see, one runs webpack every time I save a file and one just runs the server with nodemon so I don't have to type "npm start" or something of the sorts every time I save a file.
Now this works fine but I need 2 terminals always open to run it and it gets a little crowded on my screen.
And I can't have one command read like this:
"start": "npm run webpack && npm run server"
becase the webpack command is ongoing and will never reach the second command.
Is there a way to have these two commands in 1 terminal, is this even advisable?

You could run one process in the background with & (one ampersand, not two) but that would require you to manage it manually, which would be rather tedious. For details see What does ampersand mean at the end of a shell script line?.
For that use-case someone built concurrently, which makes it simple to run processes in parallel and keep track of their output.
npm install --save-dev concurrently
And your start script becomes:
"start": "concurrently 'npm run webpack' 'npm run server'"
If you want to make the output a little prettier you can give the processes names with -n and colours with -c, for example:
"start": "concurrently -n 'webpack,server' -c 'bgBlue.bold,bgGreen.bold' 'npm run webpack' 'npm run server'"

Its easy actually, use & instead of && if you don't want to wait for the first one to finish. By adding & to the end of a command, you tell to execute in the background.
Simple example
npm run webpack & npm run server
Advanced example:
I run 2 watchers (1 is a vue app, the second is a scss/js theme folder) in 1 window in 2 different folders actually. So a combination of double && and single &
(cd ~/some-path && yarn encore dev --watch) & (cd ~/some-other-path/ && yarn run dev)
The output of both watchers is mixed but is no problem actually. Only readability will decrease.
One downside is that when you hit ctrl-c to stop the processes like you used to do, it only closes the currently running non-background task. The first is running in background because it was followed by &.
So if you want to stop both processes, just close the terminal window and both will be finished.
Or hit ctrl-c to stop the open process
followed by following command to:
kill the last background process:
kill %1
OR
kill the last command
kill $!

Related

package.json scripts - I want to have it wait to run other parts of script until one part is executed fully, how to do that?

Currently I have this script in my package.json file:
"npm run build:main:dev && concurrently -k -n Main,Rend -c yellow,cyan \"electron --inspect=5858 --ignore-certificate-errors ./build --watch\" \"npm run build:renderer:watch\""
I would like to add something to it, so that it will completely finish running npm run build:main:dev before it goes on to run the rest of the script. How can I do that?

keep webpack dev-server running even after terminal closes

Hi is there a way i can keep webpack devserver running even after closing terminal.
"scripts": {
"dev-server": "npm run templates && webpack-dev-server -d --https --port 28443",
}
when i run npm run dev-server it starts but after closing terminal webpack devserver also closes is there any way to keep it running with pm2 or any other method.
use nohup
So if the script command is "dev-server" (according to your snippet), then go to your project root directory (where the package.json file is, which has your "scripts" section):
If on unix environment, just run nohup npm run dev-server &
If on windows, install git bash - it's sort of a mini unix environment for windows. And then run the nohup npm run dev-server &
This will start the webpack dev server and keep it running on the background
For me, my script section is
"scripts": {
"dev": "webpack-dev-server --open"
}
and the above command that I mentioned works fine
nohup did somehow not work for me, but i was able to make it work with forever.
sudo npm install -g forever
forever start -c "webpack serve" ./

Stop all processes depending on npm start…

I am starting multiple npm tasks in parallel (using &, not just in sequence &&). Thus in package.json:
"start": "npm run watch-blog & npm run watch-data & npm run server",
And those sub-tasks are useful stuff to me like:
"watch-blog" : "watchy -w _posts/**/* -- touch _pages/blog.md",
Question: How can I all shut down all three tasks together?
I noticed CTRL–C is only killing the last. (my
watch-blog survives and keeps „touching“)
Closing the terminal window doesn't help. Only killall node does the job, but that's killing more than I would like to…
Killing detached processes (that's the word…) will be a pain. One will have to look at pids, and more stuff coming your way. Not to mention cross-platform issues, if meant to work under windows...
Easier and working:
npm install concurrently --save
and thus
"start": "concurrently \"npm run watch-blog\" \"npm run watch-data\" \"npm run serve\"",
Tested (under Ubuntu 16.04, npm 5.6).

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)
If I understood you correctly, you want firstly run webpack and after compile run nodejs. Maybe try this:
"start": "webpack && node server/server.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"
}
Better understand the && operator
In my case the && didn't work well because one of my commands sometimes exited with non zero exit code (error) and the && chaining operator works only if the previous command succeeds.
The main chaining operators behave like this:
&& runs the next command only if the first succeeds (AND)
|| runs the next command only if the first fails (OR)
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
Others OS specific chaining operators
Other separators are different in a 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
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"
}
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"
}

npm start to run some scripts in silent mode

I have multiple scripts in package.json, most of them call some .js e.g. "copyResources" bellow. If I will run "npm run -s copyResources" in shell, it will execute silently. So far so good. But I would like to run "npm start" which will execute copyResource silently, example bellow, but this is not working (it's not silent). :(
Later I will have more scripts in "start" and I want to run some of them silently and some of them not. Thats the case why I can't just do npm start -s ...
"scripts": {
"copyResources": "echo 'Copy resources =>' && node ./bin/copyResources.js",
"start": "npm run -s copyResources"
}
Thank you very much!

Resources