In case you’ve run into this 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.
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.
The following sequential or systematic steps should be followed in order to correct the EADDRINUSE error:
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 :
lsof -i :7007
kill – 9 <PID>
On WINDOWS :
netstat -ano | findstr :7007
taskkill /PID <PID> /F
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 number
app.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