18

I have the following code to capture a ^C from the terminal and gracefully shutdown my Express app:

process.on('SIGINT', () => {
    console.log('SIGINT received ...');
    console.log('Shutting down the server');

    server.close(() => {
        console.log('Server has been shutdown');
        console.log('Exiting process ...');
        process.exit(0);
    });
});

However if I start my node instance with --inspect, then the above code fails to stop the inspector and the Chrome debugger. When I restart my application, I get the following error:

Starting inspector on 127.0.0.1:9229 failed: address already in use

How do I gracefully stop my app to avoid this error?

Full code available here.

Naresh
  • 23,937
  • 33
  • 132
  • 204

6 Answers6

51

It seems that VSCode, Puppeteer, nodemon, express, etc. causes this problem, you ran a process in the background or just closed the debugging area [browser, terminal, etc. ] or whatever , anyway, you can in CMD run

$ ps ax | grep node

then

$ killall -9 node

it is not the best solution, also, i may suggest that you look for the process using this port then send close signal

$ sudo ss -lptn 'sport = :9229'

OR

$ sudo netstat -nlp | grep :9229

OR

$ sudo lsof -n -i :9229 | grep LISTEN

THEN:

$ sudo kill <pid>

OR JUST [the two steps in one]

$ sudo kill `sudo lsof -t -i:9229`

OR

$ sudo kill $(sudo lsof -t -i:9229)
Peter B
  • 22,460
  • 5
  • 32
  • 69
KhogaEslam
  • 2,528
  • 1
  • 20
  • 21
6

This works for me:

fuser -k -n tcp 9229
Jeff Linahan
  • 3,775
  • 5
  • 37
  • 56
Gi5
  • 83
  • 1
  • 7
4

To kill all node process

taskkill /im node.exe

or forcefully

taskkill /f /im node.exe

Nikhil Vats
  • 291
  • 3
  • 7
3

I had the same terminal error from nodemon, even though I thought I'd quit all terminal processes, but simply quitting VSCode and reopening solved for me (thanks to KhogaEslam's answer for the hint).

Hope this helps someone else too!

Joel Balmer
  • 1,428
  • 2
  • 19
  • 21
0

was at a node docker. with ss, complete, if u know the port is open:

kill `ss -lptn 'sport = :THE_PORT' | tail -n1 | cut -d, -f2 | cut -d= -f2`
Tiago
  • 13
  • 4
0

I have noticed that if you are starting a script with say yarn dev then yarn will run a separate debug node process that will take the first port, then if the debugger is lenient it will try the next port and boom you have 2 ports 9229 and then next 9230.

So the solution is to actually invoke the script directly so that node debugs the actual script and assign the port you are expecting.

eg: if my package.json has "dev": "app develop" in its script section and I normally run that with yarn dev on the commandline. Doing NODE_OPTIONS=--inspect yarn dev will cause this issue. The solution will be to run NODE_OPTIONS=--inspect app develop, this invokes what yarn was going to do in the script.

Emmanuel Mahuni
  • 1,766
  • 16
  • 16