nodemon + express, listen port =? - node.js

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

in package.json
"scripts":{
// "start": "node ./bin/www"
"start": "nodemon ./bin/www"
}
the following would now be equivalent:
$ npm start
$ nodemon ./bin/www

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);
});

you too use define your for nodemon:
$ nodemon --inspect ./bin/www 3000

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');

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.

Related

React start set port number from CLI

I'm attempting to start my react server through linux CLI with a specified port number. I am NOT asking about changing the package.json script to include a defined port number.
I need to be able to start multiple react instances with different ports through CLI.
I have seen recommendations such as
npm start --PORT=4000,
npm start --PORT 4000,
npm start -- --PORT=4000
Of which none work, they all set the port to the default of 3000, or if I have a defined port in the package.json such as 5000, it defaults to that.
Whats the correct command for setting the port through CLI?
you can do it by adding PORT=4000 before react-scripts start in package.json.
"scripts": {
"start": "PORT=4000 react-scripts start"
}
then you can execute npm start
It's actually an environment variable for the port, so you can specify a PORT environment variable beforenpm start
export PORT=3005; npm start #For Linux
$env:PORT=3005; npm start #For Powershell

Run 2 MEAN js project on same ubuntu system

I want to run more than one MEAN js 0.4.2 project using grunt.
First project is running properly by second project giving below error:-
[nodemon] starting `node --debug server.js`
Fatal error: Port 35729 is already in use by another process.
Warning: Use --force to continue.
change port in /config/env/default.js
port: process.env.PORT || 3002,
Please help.I have changed default port(/config/env/default.js) from 3000 to 3002 but still giving same error.
Issue is with the nodemon, two instances of nodemon tries to run on same port.
Try to run
node server.js
Or,
you can try to configure nodemon as well.
https://github.com/ChrisWren/grunt-nodemon/issues/21#issuecomment-28116032

node.js express socket.io port 3000 in use

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.
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
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.
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 resolved the same problem with an express app doing this:
Edit the file "yourap/bin/www"
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
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
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.
for me helps to make use 3000 || 3333, and it's fix the issue
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');
});

grunt-nodemon didn't use nodemon to start server

I use express-generator create the project skeleton, and use jade, sass instead of ejs and stylus, then i use concurrent to combine the watch and nodemon like the doc, but when i run grunt tasks, output is:
[nodemon] v1.0.17
[nodemon] to restart at any time, enter rs
[nodemon] watching: fir/*/*
[nodemon] starting node bin/www
Express server listening on port 3000
mongodb connection open
it's already use node to start server, How is it?
Yes.
Grunt, nodemon and Express allways are already to use NodeJS.
Nodemon, make easy cause you don't need allways, stop and start node to run your applications. If it don't work you can force reload using rs.

nodejs + nodemon + forever give me an error

I just installed forever globally (-g). Before that I used to run with
$ npm start
Now after installed forever, I tried to lunch the node app
$ NODE_ENV=development forever nodemon server.js
but I receive this error
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
error: Cannot start forever
error: script /path/to/app/nodemon does not exist.
the same also with
$ NODE_ENV=development forever nodemon server.js
any idea?
The error you received in your output:
error: script /path/to/app/nodemon does not exist.
It appears that forever is looking for nodemon in the current working directory, and can't find it because it doesn't exist there. Try providing the absolute path when starting nodemon, which can be found with which nodemon.
forever start /usr/local/bin/nodemon server.js
Note that the start flag is what puts the application in daemon mode.
Try this
NODE_ENV=development forever start -c nodemon server.js
The -c is for execute commands, forever send you that error because it's looking for a app called nodeamon, but your app is server.js

Resources