NodeJS

Listen EADDRINUSE address already in use


In case you’ve run into this error:

NodeJS error

While working with a Node.js application (especially using frameworks like NestJS),This occurs due to a conflict of connection where the application is based using a port already in use. In this blog post, we will walk through what causes the error and the steps you can take to fix it.

What is an EADDRINUSE Error?

EADDRINUSE  is a node.js error which describes the situation as "Address in use". So it means that the application seeks to bind a particular address, in your case 127.0.0.1:7007, to a port but there is already a different process using this port.

In other words this situation is common where:

  • The application hasn’t fully shut down and thus the port is busy. 

  • There is already a process on the same port. 

  • An application attempts to assume control of the port even if it is busy.

Guide to Resolving the Error

The following sequential or systematic steps should be followed in order to correct the EADDRINUSE error:

1. Find and Terminate the Application

The easiest way around this is to locate the application that is bound to the port and end it. You may achieve this by following the below steps:

 

On LINUX or macOS :

  • Go to the terminal. Execute the command below in order to identify the application bound to the port. lsof -i :7007
  • This will display to you the PID of the application currently bound to the port 7007. After you have gotten the PID, execute this command below to end the application: kill – 9 <PID>
  • Where <PID> is replaced by the real process ID. This will end the process almost immediately and the port will be unused.

On WINDOWS :

  • Look for the Command Prompt and right click then click “run as administrator” You can execute this command in order to check which process is using whats app port netstat -ano | findstr :7007
  • This will in return show the process ID of the application that is using the port. So in order to terminate the sushi process, you use this command taskkill: taskkill /PID <PID> /F
  • Replace <PID> with the process id shown previously. This process will stop the application and will also release the port.
  •  

2. Modify the Port

If terminating the process is not feasible or if you want to use a different port, you can set the application to use a different one. If you are operating NestJS or a simple Node JS application for instance , you can assign a different port from your configuration file.

 

In case of NestJS:

In your main.ts file, you can change the port number using the app.listen() method.

const app = await NestFactory.create(AppModule);await app.listen(7008); // Change to a different port like 7008 

In Case of Standard Node JS App:

In case of using an HTTP server with the Node JS then change the port in your server.js or app.js:

const http = require('http');const app = require('./app');const port = 7008; // Change to a different port numberapp.set('port', port);const server = http.createServer(app);server.listen(port, () => { console.log(`Server is running on port ${port}`);}); 

Changing the port is a simple workaround when the original one is unavailable.

Ready to transform your business with our technology solutions? Contact Us today to Leverage Our NodeJS Expertise.

 

0

NodeJS

Related Center Of Excellence