Learn how to fix EADDRINUSE errors in Node.js when a port is already in use. Step-by-step guide with commands and code examples to resolve port conflicts
When running a Node.js server, you may encounter the error:
Error: listen EADDRINUSE: address already in use :::3000
This means another process is already using the port your application is trying to bind to. Don’t worry—this is a common issue in local development and production setups, and it’s easy to resolve.
In this guide, we’ll walk through why EADDRINUSE
occurs and how to fix it step by step.
What Causes the EADDRINUSE
Error?
The error appears because two processes cannot listen on the same port at the same time. Common reasons include:
- A previous Node.js server is still running on that port.
- Another application (like MySQL, Apache, or another Node process) is already bound to the port.
- The server crashed, but the port wasn’t released.
- You are running multiple instances of the app without handling ports properly.
Step 1: Identify Which Process Is Using the Port
On Linux / macOS:
lsof -i :3000
On Windows:
netstat -ano | findstr :3000
This will display the process ID (PID) of the program using port 3000
.
Step 2: Kill the Process Blocking the Port
Once you know the PID, terminate it:
On Linux / macOS:
kill -9 <PID>
On windows
taskkill /PID <PID> /F
Now restart your Node.js app, and the error should be gone.
Step 3: Use a Different Port
If you don’t want to kill the process, you can configure your Node.js app to run on another port:
const express = require('express');
const app = express();
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
By using process.env.PORT
, you make your app flexible for both local and cloud environments.
Step 4: Automatically Handle Port Conflicts in Code
You can add error handling to gracefully detect and fix port conflicts:
const http = require('http');
const app = require('./app'); // your Express app
const server = http.createServer(app);
const PORT = process.env.PORT || 3000;
server.listen(PORT);
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.error(`Port ${PORT} is already in use. Trying a new port...`);
server.listen(PORT + 1);
} else {
console.error(err);
}
});
Now your app will automatically try the next available port.
Step 5: Free Up Ports Automatically on Restart (Optional)
When working in development, you might want to auto-kill processes before starting a new one. Update your package.json
scripts with tools like kill-port
:
npm install kill-port --save-dev
In package.json
:
"scripts": {
"start": "kill-port 3000 && node server.js"
}
This ensures the port is cleared before starting the server.
Best Practices to Avoid EADDRINUSE
- Always stop the server before starting a new one.
- Use
nodemon
to auto-restart and release ports during development. - Prefer
process.env.PORT
to avoid hardcoding ports. - Monitor your system for lingering processes after crashes.
Conclusion
The EADDRINUSE
error in Node.js simply means a port conflict, and fixing it is straightforward: find the process, kill it, or run your app on a new port. With proper error handling and automated scripts, you can prevent this issue from disrupting your workflow.
0 Comments