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.
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 executenode ./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
After
So now I run my sever with
npm run dev
and no more errors.Try running:
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.
I resolved the same problem with an express app doing this:
find the line :
var port = normalizePort(process.env.PORT || '3000');
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
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.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):
- or -
On Ubuntu, the service is disabled with (simply):
I solved it by this:
npm install shelljs
and add code for kill nodejs process before start listen port