1

I have been using a Windows 10 laptop for years and now I changed to a MacBook. I pulled some of my code from Github (using exactly the same code for my server) and Nodemon doesn't want to restart the server automatically when I hit save. What's going on?

index.js:

const express = require("express");
const path = require("path");
const cors = require("cors");
const bodyParser = require("body-parser");
const http = require("http");

const app = express();
const server = http.createServer(app);

app.use(express.json({ extended: false, limit: "5mb" }));
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

if (process.env.NODE_ENV === "production") {
    app.use(express.static("client/build"));
    app.get("*", (req, res) =>
        res.sendFile(path.resolve(__dirname, "client", "build", "index.html"))
    );
}

const PORT = process.env.PORT || 3001;

server.listen(PORT, () => console.log(`Server started on port ${PORT}.`));

I make some changes then hit save then I get this error:

events.js:292
[0]       throw er; // Unhandled 'error' event
[0]       ^
[0] 
[0] Error: listen EADDRINUSE: address already in use :::3001

Package.json:

"scripts": {
        "start": "node index.js",
        "server": "nodemon index.js",
        "client": "npm start --prefix client",
        "dev": "concurrently \"npm run server\" \"npm run client\""
},

What I want is that when I hit save, it saves everthing then restarts the server on the same port. Everything is automatic. I do not want to kill the server manually each time I save. This one worked on Windows 10 and now it doesn't on my MacBook...

David
  • 321
  • 10
  • 23
  • sometimes it does the job, sometimes not, I installed nodemon globally – David Jun 22 '20 at 08:07
  • and when I exit, it gives me then nodemon 130 error code – David Jun 22 '20 at 08:11
  • Can you show us the contents of the package.json in the `client` directory? – eol Jun 22 '20 at 09:00
  • Also what node and npm versions are you using? – eol Jun 22 '20 at 09:19
  • I think this [question](https://stackoverflow.com/questions/43152968/nodemon-not-refreshing-browser-in-react-express-node-app) may address your issue – leonlafa Jun 22 '20 at 11:02
  • I think this [question](https://stackoverflow.com/questions/43152968/nodemon-not-refreshing-browser-in-react-express-node-app) may address your issue – leonlafa Jun 22 '20 at 11:03

2 Answers2

2

When Nodemon detects changes in any monitored files it will send a SIGUSR2 signal to the loaded application, then it respawns ("reloads") a new instance of that application in memory. If you don't handle that SIGUSR2 signal then the old copy of your application will still be live, listening for incoming requests on port 3001 (or whatever).

So the new instance is starting. It is going to listen to the same programmed port, namely 3001. But since the old copy is not yet dead (still running) then that port is still be used which will result in:

Error: listen EADDRINUSE: address already in use

That's the cause of the problem in a nutshell.

The solution is to listen to that SIGUSR2 signal and to gracefully shutdown your application programatically when that event occurs. Add the following line somewhere in your code, the earlier the better:

process.once("SIGUSR2", () => server.close(err => process.kill(process.pid, "SIGUSR2")));

Now your application will terminate itself when it sees that signal. The code above would probably not work 100%, but following this pattern one could listen to many other POSIX signals and handle them accordingly, for instance SIGINT, SIGABRT, SIGHUP, SIGTERM, etc. Read more.

Read more about Nodemon and gracefully reloading the script on the Nodemon page.

Eugen Mihailescu
  • 3,553
  • 2
  • 32
  • 29
0

Try any of the following commands to kill the process.

kill -9 $(lsof -t -i:3001)
npx kill-port 3001
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81