57

I create a simple node project using express:

npm install -g express-generator
express test
cd test/ && npm install
PORT=3000 npm start

So this gets the test app up and running on port 3000. Great. Now I'd like to use nodemon to run this project. I've installed it:

npm install -g nodemon

In the gihub README it is run the same way as node. This is a bit confusing, because the new way of starting node is npm start not node. So I tried:

$ PORT=3000 nodemon ./app.js 
13 May 23:41:16 - [nodemon] v1.0.18
13 May 23:41:16 - [nodemon] to restart at any time, enter `rs`
13 May 23:41:16 - [nodemon] watching: *.*
13 May 23:41:16 - [nodemon] starting `node ./app.js`
13 May 23:41:16 - [nodemon] clean exit - waiting for changes before restart

But when I try to connect, there's nothing there. I confirmed that with:

lsof -i TCP:3000

Which returned nothing. Normally (with npm start) it returns:

COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node    18746 user   10u  IPv4 433546      0t0  TCP *:3000 (LISTEN)

Can anyone tell whats wrong here? How is it possible to get the app to listen on the specified port with nodemon?

my setup:

npm -v
1.3.21
node -v
v0.10.24
nodemon -v
v1.0.18
express -V
4.2.0

5 Answers 5

103

in package.json

  "scripts":{
    // "start": "node ./bin/www"
    "start": "nodemon ./bin/www"
   }

the following would now be equivalent:

$ npm start
$ nodemon ./bin/www
3
  • 1
    Excellent. This answer is exactly what I was looking for. Thanks!
    – tivoni
    Commented May 14, 2014 at 23:54
  • 5
    I did not understand, how does this specify port for nodemon? I tried following this but I still face the same problem - nothing listening on port 3000
    – Suhas
    Commented Aug 8, 2015 at 12:09
  • This one does not change the listening port, it only uses nodemon to start the project than pure nodejs
    – Neo
    Commented Aug 12, 2015 at 10:05
14

This also works: Include this in your app.js (it does the same thing as the neolivz4ever said)

app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
  console.log('Express server listening on port ' + server.address().port);
});
0
4

you too use define your for nodemon:

$ nodemon --inspect ./bin/www 3000

1
  • 1
    'node --debug` and node --debug-brk are now deprecated. Use node --inspect or node --inspect-brk instead. Commented Mar 31, 2019 at 18:05
0

If you are looking for the way to specify the port number with nodemon + express-generator, go to bin/www and change the line

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

to specific number. For example,

var port = normalizePort(process.env.PORT || '1234');
-2

Additionally, some times, the port are just in use. If the other solutions not work for you, try change the port. It may be in use for some other node instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.