As I am trying to run the webservice shown as follows:
const port = 3000;
app.get('/hello', (req, res, next) => {
res.send('Hello, World!');
});
app.listen(port, () => {
console.log('listening to port', port);
});
For the very first time, it works fine. But since then, when I run node index.js
which contains the webservice mentioned above, I receive the error posted below.
To solve this issue I ran the following command:
npx kill-port 3000
but that did not solve the problem.
Please let me know if there is another way to solve this issue.
Note: I tried to use several other ports numbers, but I receive the same error message with the new port number indicated in the error message.
Error:
node:events:489
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
3
Answers
It seems that port 3000 is already in use by another process.
If you use linux as OS:
Next, kill the process using the port (check what it is first).
If you’re using Windows or macOS, there’s an equivalent way to list listening ports and kill the process using the desired port.
Windows :
MacOS:
As you mentioned, the very first time is works fine & then it throws an error second time onwards.
It has to do with the way you are stopping the application if your doing
Ctrl + Z
(orcmd + Z
on Mac) the app will be stopped on terminal but the process won’t be killed.The correct way is
Ctrl + C
(orcmd + C
on Mac), it will send SIGINT which will kill the application.Stop the process from Task Manager.
end the task