I'm running into an error in nodemon today, which only happens with esm using -r from the CLI. Here's a dead-simple reproduction:
// app.js
require('express')().listen(3000, () => console.log('Works'))
"scripts": {
"dev": "nodemon -r esm app"
},
"dependencies": {
"esm": "^3.0.84",
"express": "^4.16.4"
},
"devDependencies": {
"nodemon": "^1.18.6"
}
When I run yarn dev from the terminal, and then do Ctrl+S in app.js in the editor, I get
yarn run v1.12.3
$ nodemon -r esm app
[nodemon] 1.18.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node -r esm app index.js`
Works
[nodemon] restarting due to changes...
[nodemon] starting `node -r esm app index.js`
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1294:14)
at listenInCluster (net.js:1342:12)
at Server.listen (net.js:1429:7)
at Function.listen (/path/to/my/app/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/path/to/my/app/app.js:1)
at Object.<anonymous> (/path/to/my/app/app.js:1)
Emitted 'error' event at:
at emitErrorNT (net.js:1321:8)
at internalTickCallback (internal/process/next_tick.js:72:19)
at process._tickCallback (internal/process/next_tick.js:47:5)
at /path/to/my/app/node_modules/esm/esm.js:1:31617
at /path/to/my/app/node_modules/esm/esm.js:1:31302
at process.<anonymous> (/path/to/my/app/node_modules/esm/esm.js:1:31588)
at /path/to/my/app/node_modules/esm/esm.js:1:53673
at Function.<anonymous> (/path/to/my/app/node_modules/esm/esm.js:1:251314)
at Function.<anonymous> (/path/to/my/app/node_modules/esm/esm.js:1:251083)
at Function.<anonymous> (/path/to/my/app/node_modules/esm/esm.js:1:241320)
[nodemon] app crashed - waiting for file changes before starting...
You can see that the app boots correctly, but as soon as any file changes, nodemon can't restart it. In the meantime, the app still continues to run in the background. If I do Ctrl+C, it quits, but there's no more process on port 3000, so killing it by port fuser -k 3000/tcp doesn't do anything.
I found that
esm, i.e. if I change "dev": "nodemon -r esm app" to "dev": "nodemon app", the error goes away, and nodemon restarts correctlyesm using require("esm")(module) syntax instead of -rexpress or any web server (because the port is not used)11.2.0, 11.1.0, and 10.13.0Tried rebooting, reinstalling Node, removing yarn.lock, then removing and re-installing node_modules, locking to older versions of esm, nodemon, and express... I'm out of options here.
At first, I thought this was nodemon's fault, so I scavenged many threads with this error, but to no avail. Then I noticed that the error appears to be emitted from esm, so I thought I'd post here. Sorry, if I'm posting in a wrong repo; it seems the culprit lies in this library. Any clues are appreciated!
Hi @alex996!
Thank you for digging into this!
I could repro the issue on macOS 10.14.1 on Node 11.2.0 with yarn dev but not with npm run dev. It does look like several folks have hit the EADDRINUSE issue with nodemon and yarn outside of esm though. I noticed that if I created an empty a.js file and made the dev script "nodemon -r ./a app" that I continued to reproduce the error with yarn and not npm. I think you should file an issue on yarn using this similar repro but with the dummy a.js file.
@jdalton Thanks so much for the info. I'll follow up with the issues in nodemon.
windows 10 kill all about node.js in back
I was using nodemon 1.17.5 by following a course, Now I have updated with the latest version 1.19.x, which has fixed my problem.
I have no idea how this matter but my failing setup looked like this:
app.js file (entry point of the app)
const port = process.env.PORT || 8000;
....
....
....
server.listen(port, (error) => {
if(error) {
throw error;
}
console.log(`Express server listening on port ${port}`);
});
and my command to start the server was:
./node_modules/nodemon/bin/nodemon.js app.js
now I've changed the first line when I defined port constant from to this:
const port = process.env.PORT;
and now I'm defining the port right before starting nodemon like this:
PORT=8000 ./node_modules/nodemon/bin/nodemon.js app.js
So essentially before my process.env.PORT was not defined at all so it was taking the default 8000 defined whole assigning value to the port constant. Now it's just using the PORT defined straigh before running the server. I'd love to understand the logic behind it. Also, just running it with this port did not help. I had to remove || 8000 from the place when I declare port constant.
In my case, Whenever I am saving the code, it is showing this port is already in use.
so in that case just restart the system. You will see the magic. Error is disappear
Sometimes, it's just because nodemon is restarting faster than the process is being killed.
For example, if there is some (slow) logic in server.on('close') or server.on('SIGINT').
I have solved this issue by adding below in my package.json for killing active PORT - 4000 (in my case)
"scripts": {
"dev": "nodemon app.js",
"predev":"fuser -k 4000/tcp && echo 'Terminated' || echo 'Nothing was running on the PORT'",
}
I also got this error when I ran the project with VScode terminals and closed multiple working terminals without killing them or without stopping the server. Killing every previous terminal and starting with a new terminal solved this for me.
I had the same problem. Realized that I was using global nodemon version. Once I installed it locally, the problem was gone.
I used this to fix the same problem
process.once('SIGUSR2', function () {
process.kill(process.pid, 'SIGUSR2');
});
https://www.npmjs.com/package/nodemon
Controlling shutdown of your script
Thank's @AbdelhakAj, It works!
Most helpful comment
I was using nodemon 1.17.5 by following a course, Now I have updated with the latest version 1.19.x, which has fixed my problem.