44

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

> nodekb@1.0.0 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!

Mat
  • 713
  • 1
  • 5
  • 9
  • 2
    What kind of terminal do you use? Have you tried bash on Windows? Have you been waiting for a while? I remember that node on Windows machines is quite slow. – lumio Jun 27 '17 at 20:12
  • 3
    It's an issue with bash for Windows and there are quite a few issues on GitHub around it [Like](https://github.com/cbucher/console/issues/273) – Shivam Jun 27 '17 at 20:18
  • I tried it on normal windows cmd, and it worked as it should there. Looks like it's a problem with git bash. Thanks for the help! – Mat Jun 27 '17 at 20:29
  • If someone using docker ends up finding this, then there's a docker-based solution: https://stackoverflow.com/questions/52518477/why-cant-i-always-kill-a-docker-process-with-ctrl-c – Multihunter Sep 18 '19 at 03:24

13 Answers13

32

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

Pankaj Shinde
  • 3,361
  • 2
  • 35
  • 44
17

I tried it on normal windows cmd, and it worked as it should there. Looks like it's a problem with git bash.

Mat
  • 713
  • 1
  • 5
  • 9
  • 1
    This just started to happen to me 1 month ago or so, not sure why and is with all bash clients for windows (tried many). It works in win cmd, strangely enough works in git bash for my colleagues... – Tiberiu Popescu Oct 05 '17 at 14:50
  • 2
    This indeed seems to be an issue with node and Git bash on Windows. It would be nice if the issue were tracked somewhere. I'm not sure if it is a node issue or a git for windows issue. – vossad01 Dec 12 '17 at 18:59
  • 1
    Using CMD, PS, or Git Bash all give me the same issue – Janac Meena Mar 05 '18 at 14:59
  • I still have the issue with Git Bash, don't know if it is something not properly confiugred in node, but it always hangs when using npm start with GitBash – Mese Apr 20 '18 at 11:21
  • Just adding I am now seeing this issue on windows cmd, it only seems to happen when I run more than 1 though and am forced onto another port#. – Fiddle Freak Apr 27 '20 at 16:47
  • If Ctrl + C doesn't work, you can try pressing Ctrl + Q keys in Git Bash. In my PC, it stops the npm server, So, it works for me. – Md. Ehsanul Haque Kanan Jul 14 '20 at 13:44
5

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 customized msys2-runtime to have a fully working git inside MSYS2.

Here the steps to take:

  1. Open an MSYS2 terminal.
  2. Edit /etc/pacman.conf and just before [mingw32] (line #71 on my machine), add the git-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
    
  3. 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
    
  4. Then synchronize new repository

    pacboy update
    
  5. 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.

  6. Then synchronize again (updating the non-core part of the packages):

    pacboy update
    
  7. And finally install the Git/cURL packages:

    pacboy sync git:x git-doc-html:x git-doc-man:x git-extra: curl:x
    
  8. Finally, check that everything went well by doing git --version in a MINGW64 shell and it should output something like git 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

Birchlabs
  • 7,437
  • 5
  • 35
  • 54
5

Use Ctrl+\ to send the SIGQUIT signal. It will close the server.

Reference - https://en.wikipedia.org/wiki/Signal_(IPC)

Surya Singh
  • 139
  • 2
  • 11
4

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.

  1. Open Task Manager
  2. Go into the Processes tab
  3. Look for node.exe and then press End Process

This has allowed me to stop the server quickly.

Gene Parcellano
  • 5,799
  • 5
  • 36
  • 43
4

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.

YahiaRefaiea
  • 172
  • 1
  • 12
  • This appears to be fixed in the latest version (2.19.1) of Git Bash. Thanks for pointing to the relevant issue! – Jonny Asmar Dec 10 '18 at 04:27
3

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]

2

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.

Reactgular
  • 52,335
  • 19
  • 158
  • 208
0

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.

Janac Meena
  • 3,203
  • 35
  • 32
0

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 &.

10 Rep
  • 2,217
  • 7
  • 19
  • 33
0

if you use Node.js Exec Extention to run your project from f8, you can use also f9 to cancel running..

-1

Use Ctrl + C, then input: >pm2 stop all

This will stop all server or when you get stack with nodejs.

M.Quan
  • 1
-3

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

lunchtime
  • 55
  • 5