4

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

> express-locallibrary-tutorial@0.0.0 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! express-locallibrary-tutorial@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the express-locallibrary-tutorial@0.0.0 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.

JimB814
  • 373
  • 4
  • 19
  • If you are on Windows, check task manager to see if any other node processes are running. If you are on *nix, run `ps -A | grep node` to see what node processes are still running. – zero298 Sep 19 '17 at 17:52
  • If you are on MING64 you should try [this thread](https://stackoverflow.com/questions/48198/how-can-you-find-out-which-process-is-listening-on-a-port-on-windows) in a Windows' cmd.exe – gogaz Sep 19 '17 at 17:55
  • Thanks for the responses. I use Bash & Command Prompt. `C:\> netstat -a` returns about 45 127.0.0.1 references, but none include ports I've been using. Some that I have been using are listed under the Local Address 0.0.0.0. I see 0.0.0.0:3000, 0.0.0.0:3300, 0.0.0.0:3302, 0.0.0.0:3303. The Foreign Address for each is Jim-PC, (my machine), & the State is "Listening." Getting warmer? I do not understand the meaning of what I'm looking at, but seeing the port numbers suggests that my PC is using & saving 0.0.0.0:port#, with a "Listening" status. If so, how can it be fixed? – JimB814 Sep 19 '17 at 18:40
  • Something in your app is spawning your server and then not cleaning up after itself whenever your app is closed. You would need to post your server creation code to try and figure out what is causing this. To resolve the taken ports, use [`netstat -a -b`](https://stackoverflow.com/a/48199/691711) to list the port **and** the executable using it. Then use process manager to kill the offending process. – zero298 Sep 19 '17 at 18:54
  • The link @gogaz provided has the command -- netstat -aon | find /i "listening". It returns just what's listening, so the list is more manageable. I ran netstat -a -b, and it returned a lengthy list. This comment box doesn't allow me to copy a screenshot. I do not know how to use process manager to kill anything. The 0.0.0.0 ones I listed above include node.exe in this list. – JimB814 Sep 19 '17 at 19:01
  • I re-booted. The server @port 3303 started. `netstat -a -b` returns a list that excludes the port I am using (127.0.0.1:3303), but does list 0.0.0.0:3303 as listening. Somehow, my machine is registering the wrong ip address. – JimB814 Sep 19 '17 at 19:24
  • Are you terminating your node process correctly? https://stackoverflow.com/questions/5266152/how-to-exit-in-node-js – Click Ahead Sep 19 '17 at 20:02
  • run `msconfig` and investigate tabs `Services` and `Startup` for any instance of node or whatever that could listen on the given port. the `0.0.0.0:port` means the targeted process is listening all incomming IPs – gogaz Sep 19 '17 at 20:32
  • @ClickAhead. I use Ctrl+C to stop the server. I type "exit" to close the terminal (nice link). – JimB814 Sep 19 '17 at 21:48
  • @gogaz I looked thru Services & Startup. No instances of node. Nothing unusual or new. Mostly Microsoft. I don't see port numbers. In Service I disabled bluetooth related services. – JimB814 Sep 19 '17 at 22:18
  • As a diagnostic test, try changing the port number in the code before you stop the server. If the process is somehow staying alive it should stay on the original port, whereas if it's being automatically restarted I would imagine it would pick up the new port at that point. As another test I would try connecting to that port from a web browser, see what's listening and how it responds. – skirtle Sep 19 '17 at 23:09
  • @skirtle. Thanks! Stopped server (port 3303). Started server & rec'd error that port already in use. Set port in code to 3304. Started server - no error. Refresh - connection persists. Changed port in code to 3305, saved. Refreshed browser and the connection persists (http://localhost:3304). Changed to http://localhost:3305 - no connection (which is correct since a change requires restarting server). Returned to http://localhost:3304 - connection is there. Next, changed browsers, entered http://localhost:3304 and have connection. Not sure what this means. – JimB814 Sep 20 '17 at 11:57
  • I changed port to 3308 in code. Saved. Started server. Connection established. Terminal displays (node:13460). Ran `netstat -aon | find /i "listening"` in Command Prompt. Found '13460' in the far right of five columns in two rows with the following data: [1st row]: (Col 1) TCP, (Col 2) 0.0.0.0:3308, (Col 3) 0.0.0.0, (Col 4) LISTENING, (Col 5) 13460. [2nd row]: (Col 1) TCP, (Col 2) [::]:3308, (Col 3) [..]:0, (Col 4) LISTENING, (Col 5) 13460. Found another row with '3308'. (continued next comment...) – JimB814 Sep 20 '17 at 12:50
  • (...cont from previous comment) Also, rows exist with ports previously used, e.g. 3303, 3304, 3305, 3306, 3307. These rows each have Col 1 = TCP, Col 2 = 0.0.0.0:port#, Col 3 = 0.0.0.0, Col 4 = LISTENING, Col 5 = different numbers ( Col 5 value in row with 0.0.0.0:3308 = 13460). I hope this helps in finding a solution. The same rows exist further down where Col 2 = [::]. E.g [::]:3308. Thanks. – JimB814 Sep 20 '17 at 12:50
  • Update: I was using express-generator for the 1st time. I am unsure if or how the www file is or is not related to this issue, but I (1) commented out all the code in www, (2) added the app.listen code and port handling code into app.js, (3) removed the export of app from app.js. To start the server, I am executing `node app.js`. To stop the server -- Ctrl+c. I have stopped and started the server now many times using port 3309 without errors. Each time I re-start the server, it returns a new node number. (...continued next comment...) – JimB814 Sep 20 '17 at 13:55
  • (...cont from previous comment) Right now it's 12176. I ran `netstat -aon | find /i "listening"` and in the row where Col 2 = 0.0.0.0:3309, the last column = 12176. I stopped the server, re-started, with a new node number of 8916. I ran `netstat -aon | find /i "listening"` again and in the row where Col 2 = 0.0.0.0:3309, the last column = 8916. In the browser, 0.0.0.0:3309 does not work, but 127.0.0.1:3309 displays as expected. Yet, in what's returned by `netstat -aon | find i/ "listening` 127.0.0.0:3309 is not listed, whereas, 0.0.0.0:3309 is listed. – JimB814 Sep 20 '17 at 13:55

1 Answers1

8

Here we are in 2020 and this issue seems to not have been fully solved.

I'm writing this workaround for nodemon here, because at the present time this is the first result on google for the query nodemon The port or ip 0.0.0.0 is already in use.

  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 (replace 18000 with your port, eg 3000):

    { "events": { "restart": "kill-port 18000", "crash": "kill-port 18000" }, "delay": "1500" }

  3. Then, in your package.json file, have something like this:

    "scripts": { "start-dev": "nodemon app.js", },

Then start your app in dev mode with npm run start-dev

Tudor Constantin
  • 24,065
  • 7
  • 44
  • 66