1

When I save my files with nodemon, it gives me an error saying

events.js:292
[0]       throw er; // Unhandled 'error' event
[0]       ^
[0] 
[0] Error: listen EADDRINUSE: address already in use :::9000
[0]     at Server.setupListenHandle [as _listen2] (net.js:1313:16)
[0]     at listenInCluster (net.js:1361:12)
[0]     at Server.listen (net.js:1447:7)
[0]     at Function.listen (/Users/KrithinPakshootra/Documents/Project Code/calcu-api/node_modules/express/lib/application.js:618:24)
[0]     at Object.<anonymous> (/Users/KrithinPakshootra/Documents/Project Code/calcu-api/server.js:15:5)
[0]     at Module._compile (internal/modules/cjs/loader.js:1200:30)
[0]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
[0]     at Module.load (internal/modules/cjs/loader.js:1049:32)
[0]     at Function.Module._load (internal/modules/cjs/loader.js:937:14)
[0]     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
[0]     at internal/main/run_main_module.js:17:47
[0] Emitted 'error' event on Server instance at:
[0]     at emitErrorNT (net.js:1340:8)
[0]     at processTicksAndRejections (internal/process/task_queues.js:84:21) {
[0]   code: 'EADDRINUSE',
[0]   errno: -48,
[0]   syscall: 'listen',
[0]   address: '::',
[0]   port: 9000
[0] }
[0] [nodemon] app crashed - waiting for file changes before starting...

Usually I close my Postman requests to the api, but now it's staying. I looked what's running on port :9000 with sudo lsof -i :9000, but shows only my backend.I'm using concurrently and express.

Though sometimes if I save several times, then it goes back to normal.

My server.js code:

const express = require('express');
const cors = require('cors');

const connectDB = require('./config/db');

const app = express();
connectDB();
app.use(express.json({ extended: false }));
app.use('/api/history', cors(), require('./routes/api/history'));
app.use('/api/signup', cors(), require('./routes/api/signup'));
app.use('/api/auth', cors(), require('./routes/api/auth'));
app.use('/api/login', cors(), require('./routes/api/login'));
app.use('/api/points', cors(), require('./routes/api/points'));
app.use('/api/history', cors(), require('./routes/api/history'));
app.listen(9000);
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
  • Does this answer your question? [Node / Express: EADDRINUSE, Address already in use - Kill server](https://stackoverflow.com/questions/4075287/node-express-eaddrinuse-address-already-in-use-kill-server) – dhruv479 Mar 30 '21 at 12:13

1 Answers1

2

You can fix the issue by adding --delay 500ms where you are calling nodemon yourEntryPointFile.js so your command in package.json should look like nodemon --delay 500ms yourEntryPointFile.js.

Plus add this too inside the server file, to control the shutdown of your script:

process.once('SIGUSR2', function () {
  process.kill(process.pid, 'SIGUSR2');
});

Nicolae Maties
  • 2,476
  • 1
  • 16
  • 26