0

Node JS givs me this error,

events.js:136
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::3000
    at Object._errnoException (util.js:999:13)
    at _exceptionWithHostPort (util.js:1020:20)
    at Server.setupListenHandle [as _listen2] (net.js:1379:14)
    at listenInCluster (net.js:1420:12)
    at Server.listen (net.js:1508:7)
    at Function.listen (/opt/bitnami/apps/direcDem/node_modules/express/lib/application.js:618:24)
    at MongoClient.connect (/opt/bitnami/apps/direcDem/server.js:16:6)
    at connectCallback (/opt/bitnami/apps/direcDem/node_modules/mongodb/lib/mongo_client.js:314:5)
    at /opt/bitnami/apps/direcDem/node_modules/mongodb/lib/mongo_client.js:252:13
    at process._tickCallback (internal/process/next_tick.js:150:11)
[nodemon] app crashed - waiting for file changes before starting...

This is a very common error on SO, it seems, but the thing is that the app still works in spite of this error being thrown and the last line that says that the app has crashed. Also, this error does not always throw.

This is my very simple server.js:

const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const bodyParser = require('body-parser');
const db = require('./config/db');

const app = express();

const port = 3000;

app.use(bodyParser.urlencoded({ extended: true }));

MongoClient.connect(db.url, (error, database) => {
    if (error) return console.log(error)
    require('./app/routes')(app, database);

    app.listen(port, () => {});
})

I think the error has to do with the Mongo database that I am using, but I'm not sure what the problem is. The CRUD operations still work, so I have no idea what's going wrong.

The error is problematic because

  1. I don't like errors
  2. It blocks other console.logs

Why is this error happening, and how can I fix it?

IHaveAQuestion
  • 729
  • 8
  • 19
  • You are already using port 3000. change to any other port – SunriseM Jan 21 '18 at 17:59
  • @SunriseM, why would this error sometimes throw but not always, and why does the app work regardless? – IHaveAQuestion Jan 21 '18 at 18:01
  • My guess is that somehow you are running the app twice (maybe because nodemon?) so the first one runs and the other one crash because the first is already using port 3000 – SunriseM Jan 21 '18 at 18:03
  • @SunriseM, is it possible to check if there are any running ones? I checked `forever list` and it is empty, and my command line is freed up. – IHaveAQuestion Jan 21 '18 at 18:04
  • if you are on linux [this](https://stackoverflow.com/questions/21575808/find-node-js-instance-on-ubuntu) can help you, use task manager if you are in windows. [Nodemon github issue](https://github.com/remy/nodemon/issues/1025) – SunriseM Jan 21 '18 at 18:15

0 Answers0