How to Fix Error: Listen Eaddrinuse While Using Nodejs

How to fix Error: listen EADDRINUSE while using NodeJS?

EADDRINUSE means that the port number which listen() tries to bind the server to is already in use.

So, in your case, there must be running a server on port 80 already.

If you have another webserver running on this port you have to put node.js behind that server and proxy it through it.

You should check for the listening event like this, to see if the server is really listening:

var http=require('http');

var server=http.createServer(function(req,res){
res.end('test');
});

server.on('listening',function(){
console.log('ok, server is running');
});

server.listen(80);

How to fix Error: listen EADDRINUSE while using NodeJS?

EADDRINUSE means that the port number which listen() tries to bind the server to is already in use.

So, in your case, there must be running a server on port 80 already.

If you have another webserver running on this port you have to put node.js behind that server and proxy it through it.

You should check for the listening event like this, to see if the server is really listening:

var http=require('http');

var server=http.createServer(function(req,res){
res.end('test');
});

server.on('listening',function(){
console.log('ok, server is running');
});

server.listen(80);

NodeJS: Error found: listen EADDRINUSE :::3000?

Try setting the --runInBand flag, this will run your tests sequentially.

https://jestjs.io/docs/en/cli#runinband

In package.json scripts:

"scripts": {
...
"test": "jest --watchAll --verbose --runInBand",
...
}

[UPDATE] On further investigation, while you can use --runInBand to run your tests sequentially, another option is to await the server.close() since it returns a promise. As such, in your afterEach:

...
await server.close();
...

[UPDATE] I believe a better way to solve this issue is to separate out the index.js file into an index.js and a server.js file. This enables you to pull your index.js file into your tests without .listen:

// index.js
const express = require("express");
const app = express();

app.get("/", (req, res) => {
res.status(200).send({ hello: "world!" });
});

module.exports = app;

Then in a separate server.js file:

const server = require("./index");

const PORT = process.env.PORT || 3001;

server.listen(PORT, () => {
console.log(`App running on: ${PORT}`);
});

module.exports = server;

When running your app, you run: node server.js.

This then enables you to test your routes by pulling your index.js file into your test as follows:

// test.js
const request = require("supertest");
const server = require("./index");

describe("GET /", () => {

test("should return status 200", async () => {
const res = await request(server).get("/");
expect(res.status).toBe(200);
});

});

Error: listen EADDRINUSE: address already in use 3000;

Changing the app.js file to

const express = require('express');
const app = express();
const port = process.env.PORT || 3000;

require('dotenv').config();

app.get('/', (req, res) => {
res.send('Hello World!!!');
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

resolves the issue, but PORT in .env is not being read...

ERROR with npm start. Error: listen EADDRINUSE: address already in use :::5000

I finally solved this by updating my mac system. I think there must be some problem with my airplay function in the old system version



Related Topics



Leave a reply



Submit