jworkman / ASL-Knowledge-Base

Centralized knowledge base for students in Advanced Server-side Languages.
1 stars 1 forks source link

Getting the error "EADDRINUSE" in my terminal #1

Open jworkman opened 11 months ago

jworkman commented 11 months ago

Description of issue

For some reason when attempting to restart my NodeJS server I am getting a strange EADDRINUSE error message in my console output.

How to reproduce

Simply change directory into the project folder, and run the following command:

node index.js

Software used

Screenshots

Screenshot from 2023-12-06 12-08-12

Backtrace of error

  node:events:491
  throw er; // Unhandled 'error' event
  ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (node:net:1740:16)
    at listenInCluster (node:net:1788:12)
    at Server.listen (node:net:1876:7)
    at Function.listen (/home/jworkman/Classes/Curriculum/WDV442/asl-project/node_modules/express/lib/application.js:635:24)
    at Object.<anonymous> (/home/jworkman/Classes/Curriculum/WDV442/asl-project/index.js:6:5)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1767:8)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
        code: 'EADDRINUSE',
        errno: -98,
        syscall: 'listen',
        address: '::',
        port: 3000
    }

Research performed

So far what I've learned from a few stack overflow threads like this one. Is that this issue is being caused by two of the same processes trying to run on the same port. In my case I didn't realize that I still had an old node index.js process running in an old terminal tab that I forgot to close out. When I attempted to run the process again in a new terminal tab, NodeJS procuced that error since it could not bind to port 3000 because it was already in use by another process (hence the error EADDRINUSE).

BradleyMatera commented 1 month ago

Issue: Address Already in Use (EADDRINUSE)

•Description: While running my tests, I came across the error: listen EADDRINUSE: address already in use :::8080. •Root Cause: This error occurred because port 8080 was already occupied, possibly due to another instance of the API running or a Docker container still holding onto that port. •Resolution: I fixed the issue by stopping all active Docker containers and checking for any lingering processes using port 8080. To identify and terminate these processes, I used the following commands:

lsof -i :8080 kill -9

•Alternative Solution: Instead of manually checking for port conflicts, I added logic to my test setup to dynamically assign a free port. By passing 0 to app.listen(), the operating system chooses an available port for the tests. Here’s the refined structure:

let server;

beforeAll((done) => { server = app.listen(0, () => { const port = server.address().port; // Get the dynamically assigned port console.log(Running tests on dynamic port: ${port}); done(); }); });

afterAll(async () => { await mongoose.connection.close(); server.close(); });

This adjustment prevents the recurring “port already in use” issue by automatically assigning a free port for each test run, avoiding the need to manually track and free up ports. It’s a more reliable approach, especially when dealing with dynamic environments.

This code is part of my codebase and is available for others to use, but please remember to follow the FSU guidelines regarding code sharing and attribution.