JimB814
JimB814

Reputation: 510

Port "already in use" after each server restart

Searching for help with peculiar issue. Each time I stop and restart the server, I get this message:

Jim@Jim-PC MINGW64 ~/nodeProjects/express-locallibrary-tutorial
$ npm start

> [email protected] start C:\Users\Jim\nodeProjects\express-locallibrary-tutorial
> node ./bin/www

Port 3300 is already in use
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Jim\AppData\Roaming\npm-cache\_logs\2017-09-19T17_39_10_578Z-debug.log

I change the port to 3301, it works. I stop and restart the server and receive an error that 3301 is already in use. I change the port to 3302 and it works. Stop and start and get error that 3302 is already in use.

I stopped using nodemon several days ago for the same issue on another application, and it stopped. For this application, I installed nodemon, but not globally, ran into issues and uninstalled it. But the issue persists.

I would appreciate any ideas on a solution if anyone has encountered this.

Upvotes: 5

Views: 5568

Answers (3)

Khadim H.
Khadim H.

Reputation: 645

Open your CMD on your laptop and first run this command
netstat -ano | findstr :7001
Like!
C:\Users\Hp 850g1>netstat -ano | findstr :7001

and then run this command
taskkill /PID 2820 /F
Like!
C:\Users\Hp 850g1>taskkill /PID 2820 /F
enter image description here

Upvotes: 0

Burak Bayraktaroğlu
Burak Bayraktaroğlu

Reputation: 53

Using nodemon with parameter --signal SIGKILL is fixed the issue for me.

Like;

"scripts": {
    "nodemon": "nodemon --signal SIGKILL"
  }

But I also had nodemonConfig before, as follows;

"nodemonConfig": {
    "exec": "npm run startDev",
    "watch": [
      "src/*"
    ]
  },

Upvotes: 1

Tudor Constantin
Tudor Constantin

Reputation: 26861

If you're using nodemon - you can handle like this:

  1. Install the kill-port node package as a dev dependency:

    npm install kill-port --save-dev
    
  2. Create a nodemon.json file in the root of your project containing:

    {
      "events": {
        "restart": "kill-port 3000",
        "crash": "kill-port 3000"
      },
      "delay": "1500"
    }
    
  3. Then, in your package.json file, have something like this:

    "scripts": {
        "start-dev": "nodemon app.js",
    }
    
  4. Then start your app in dev mode with:

    npm run start-dev
    

Upvotes: 15

Related Questions