Reputation: 793
When I start my server with node app.js
in the command line (using Git Bash), I can stop it using ctrl + C.
In my package.json file i got this start-script that allows me to use the command npm start
to start the server:
"scripts": {
"start": "node app"
},
When I do this, the server starts as normal:
$ npm start
> [email protected] start C:\Projects\nodekb
> node app.js
Server started on port 3000...
But when i ctrl + C now, the server does not get stopped (the node process still remains in task manager). This means that I get an error when I try to do npm start
again, because port 3000 is still being used.
I'm following a tutorial on youtube (video with timestamp), and when this guy ctrl + C and then runs npm start
again, it works as normal.
Any ideas why my server process is not stopped when I use ctrl + C?
My app.js file if needed:
var express = require("express");
var path = require("path");
//Init app
var app = express();
//Load View Engine
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "pug");
//Home Route
app.get("/", function(req, res) {
res.render("index", {
title: "Hello"
});
});
//Add route
app.get("/articles/add", function (req, res) {
res.render("add_article", {
title: "Add Article"
});
});
//Start server
app.listen(3000, function() {
console.log("Server started on port 3000...");
});
Thanks!
Upvotes: 52
Views: 52849
Reputation: 411
I still experience this today on windows 11 with cmd.exe
.
After much trials and none worked, i switched to windows powershell
as the default command line tool and it worked, also a trial with gut bash
worked.
While using windows powershell
or git bash
it worked as expected and ctrl + c
gracefully killed the current process
Upvotes: 0
Reputation: 3689
Ctrl + C does not kill the server. The resolution to the issue was using following code snippet in server.js:
process.on('SIGINT', function() {
console.log( "\nGracefully shutting down from SIGINT (Ctrl-C)" );
// some other closing procedures go here
process.exit(0);
});
This worked for me.
You can also check for other solutions mentioned at Graceful shutdown in NodeJS
Upvotes: 43
Reputation: 1
Inside package.json under scripts I had this line react-scripts start&
. Notice it ends with an &
which would send the process to the background and ctrl+c
will not work. Somehow trying to bring this to the foreground with fg
also did not work. Solved the problem by removing the &
.
Upvotes: 0
Reputation: 54741
I was able to fix this by switching to nodemon to run the server.
npm install --save-dev nodemon
package.json:
"scripts": {
"start": "nodemon app"
},
I was trying to get json-server
to quit a custom server script, but it always left a child process running on Windows. It seems to be a specific problem running express
via npm
on Windows. If you run the server directly via the c:>node server.js
then it seems to quit correctly.
Upvotes: 2
Reputation: 189
Use Ctrl+\ to send the SIGQUIT signal. It will close the server.
Reference - https://en.wikipedia.org/wiki/Signal_(IPC)
Upvotes: 9
Reputation: 8046
I encountered this problem in MSYS2 proper, even in latest build (x64 2018-05-31).
Luckily, Git for Windows maintain a customized MSYS2 runtime. They have patches that have not been sent upstream, including a patch that fixes emulation of SIGINT, SIGTERM and SIGKILL.
Discussion: https://github.com/nodejs/node/issues/16103
I was able to make my "MSYS2 proper" platform use Git for Windows' MSYS2 runtime, by following these instructions.
Repeated here for posterity:
Install inside MSYS2 proper
This guide assumes that you want the 64-bit version of Git for Windows.
Git for Windows being based on MSYS2, it's possible to install the
git
package into an existing MSYS2 installation. That means that if you are already using MSYS2 on your computer, you can use Git for Windows without running the full installer or using the portable version.Note however that there are some caveats for going this way. Git for Windows created some patches for
msys2-runtime
that have not been sent upstream. (This had been planned, but it was determined in issue #284 that it would probably not be happening.) This means that you have to install Git for Windows customizedmsys2-runtime
to have a fully workinggit
inside MSYS2.Here the steps to take:
- Open an MSYS2 terminal.
Edit
/etc/pacman.conf
and just before[mingw32]
(line #71 on my machine), add thegit-for-windows
packages repository:[git-for-windows] Server = https://wingit.blob.core.windows.net/x86-64
and optionally also the MINGW-only repository for the opposite architecture (i.e. MINGW32 for 64-bit SDK):
[git-for-windows-mingw32] Server = https://wingit.blob.core.windows.net/i686
Authorize signing key (this step may have to be repeated occasionally until https://github.com/msys2/msys2/issues/62 is fixed)
curl -L https://raw.githubusercontent.com/git-for-windows/build-extra/master/git-for-windows-keyring/git-for-windows.gpg | pacman-key --add - && pacman-key --lsign-key 1A9F3986
Then synchronize new repository
pacboy update
This updates
msys2-runtime
and therefore will ask you to close the window (not just exit the pacman process). Don't panic, simply close all currently open MSYS2 shells and MSYS2 programs. Once all are closed, start a new terminal again.Then synchronize again (updating the non-core part of the packages):
pacboy update
And finally install the Git/cURL packages:
pacboy sync git:x git-doc-html:x git-doc-man:x git-extra: curl:x
Finally, check that everything went well by doing
git --version
in a MINGW64 shell and it should output something likegit version 2.14.1.windows.1
(or newer).
Note: I found that the git-extra
package installed by step 7 was quite intrusive (it adds a message "Welcome to the Git for Windows SDK!" to every terminal you open), so I removed it with pacman -R git-extra
.
Note 2: I also found that Git for Windows' MSYS2 runtime opens in a different home directory than did MSYS2 proper's. This also means it reads in the wrong bash profile. I fixed this by adding an environment variable to Windows in the Control Panel: HOME=/C/msys64/home/myusername
Upvotes: 5
Reputation: 19
Use Ctrl + C, then input: >pm2 stop all
This will stop all server or when you get stack with nodejs.
Upvotes: -2
Reputation: 3557
I was able to debug this issue by checking the ports using TCP View, and realizing that my Node server was running even though I had pressed ctrl-C to stop it. I suggest killing the terminal you are running node from entirely.
Upvotes: 0
Reputation: 312
I had the same problem working with npm
. But finally, I knew it was a problem with git itself.
There was a comment by dscho on GitHub 15 days ago. He said that they're working to fix this problem in the next release. He also shared the exact msys-2.0.dll
file that can fix the problem for the people who can't wait.
Personally, I couldn't wait :p. So, I gave it a try, downloaded the file, and throw it in the git folder as he said. And the problem gone! It was awesome!
But please be sure to take a backup before you replace the file.
I also tried to kill it after running express
as I used to; using taskkill /im node.exe
on the cmd
but there was no process to be found.
Check out this issue on GitHub,and search for the name of the file msys-2.0.dll
to get to the comment faster.
Upvotes: 4
Reputation: 6044
I use git bash on my Windows machine and have run into this issue in the last month or so.
I still do not know what's causing it but I've found another way to stop it.
This has allowed me to stop the server quickly.
Upvotes: 3
Reputation: 793
I tried it on normal windows cmd, and it worked as it should there. Looks like it's a problem with git bash.
Upvotes: 17
Reputation: 92
Sometimes the node process hangs.
Check for the process ID using ps
You may want to grep for node and then kill the process using kill -9 [PID]
Upvotes: 3
Reputation: 65
This is more than likely just a problem with your console not accurately sending the command to the process. This is pretty common, especially when using third party consoles like cmdr / conemu.
The solution?
Just hit ctrl+c several times until it closes :P
Upvotes: -3