14

I've been following this(http://socket.io/get-started/chat/) tutorial on how to make a simple chat application using socket.io.

I tried to however use Express to create it and I was wondering why port 3000 is already in use? The code below will not work unless I change the port number.

/* Make the http server listen on port 3000. */
http.listen(3000, function(){
 console.log('listening on *:3000');
});

Does express use the port to do other things like routing or something? Is there a simple way to find what is happening on that port?

I may also be doing something dodgy with my require things:

var express = require('express');
var app = require('express')();
var http = require('http').Server(app);
var router = express.Router();
var io = require('socket.io')(http);

Thanks.

himahimahima
  • 397
  • 2
  • 4
  • 10
  • Do you have previous run of that program still going? That would be the most likely thing, particularly if ports other than 3000 work. – Aaron Dufour Jan 28 '15 at 04:52
  • @himahimahima Are you on Mac OS X or Linux? Try `sudo lsof -i | grep 3000` to check whether any other service is using the port, and kill it if there is one. – Daniel Le Jan 28 '15 at 06:10
  • @DanielLe I'm on a Linux machine - I've tried that and "sudo lsof -i | grep 3000" both dont show anything – himahimahima Jan 28 '15 at 06:14
  • @himahimahima The easiest solution is to restart your machine, or change your code to use a different port. – Daniel Le Jan 28 '15 at 06:16
  • Sorry to bump an old thread - but is the easiest solution really to restart your machine!? Ideally, the code would always tidy up / kill the server when it gets a kill command. Is there a way to do that? – GrayedFox Jan 14 '16 at 20:57
  • GrayedFox, check this out. Although I should add that it did NOT work for me on Mac OSX: http://stackoverflow.com/questions/23258421/how-to-stop-app-that-node-js-express-npm-start – Nitzan Wilnai Mar 06 '16 at 16:11

8 Answers8

27

I ran into this problem too and I solved it by this:

Do not use npm start to start your web app

Use node app.js instead

Ethan Yanjia Li
  • 711
  • 8
  • 13
  • 1
    I had a similar problem when trying to work with multiple express.js apps at the same time (all of them where trying to use port 3000). "node app.js" solved this problem as well. – Eduardo Casas Apr 09 '15 at 11:46
12

Try running:

netstat -anp tcp | grep 3000

This should show you the name of the process that is using port 3000. Here's another issue on StackOverflow that covers this issue in more depth.

Community
  • 1
  • 1
Brian Tovar
  • 344
  • 1
  • 3
4

One of the best way to do this during the development would be through IDE where you can do comprehensive debugging and step through the code.

If you are using WebStorm, this works. From run configurations -> Edit Configurations -> Nods.js and add the app.js as the node parameter. See below arrow in the screenshots for more details.

enter image description here

Gajen Sunthara
  • 4,470
  • 37
  • 23
2

I resolved the same problem with an express app doing this:

  1. Edit the file "yourap/bin/www"
  2. find the line :

    var port = normalizePort(process.env.PORT || '3000');

  3. replace it by:

    var port = normalizePort('XXXX');

where XXXX is the port number you want to use

Then youre free to do npm start! xD

2

I had (forgotten that I had) previously installed ntop, which by default also uses port 3000, and was therefore getting the same error as described here.

As others have mentioned, use netstat or lsof to find the offending service (and prefix the command with sudo, to get the correct process name):

sudo lsof -P | grep ':3000'

- or -

sudo netstat -anp tcp | grep 3000

On Ubuntu, the service is disabled with (simply):

service ntop stop
Dan0
  • 536
  • 6
  • 5
2

Similar to answer above to not use npm start.

I was using nodemon and with expressjs and expressjs generator. I was using nodemon to execute npm start, while npm start itself execute node ./NodeApp/bin/www

So i edited to make nodemon to execute node ./NodeApp/bin/www by itself and that error go away.

Conclusion

Before

package.json

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node ./NodeApp/bin/www",
    "build": "webpack --watch",
    "dev": "nodemon --exec npm start"
},

After

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --watch",
    "dev": "nodemon --exec node ./NodeApp/bin/www"
},

So now I run my sever with npm run dev and no more errors.

cjmling
  • 6,896
  • 10
  • 43
  • 79
0

I solved it by this:

npm install shelljs

and add code for kill nodejs process before start listen port

var shell = require('shelljs');
shell.exec("pkill nodejs");
shell.exec("pkill node");

/* Make the http server listen on port 3000. */
http.listen(3000, function(){
 console.log('listening on *:3000');
});
Utopia
  • 663
  • 7
  • 8
-1

for me helps to make use 3000 || 3333, and it's fix the issue

chavy
  • 841
  • 10
  • 20