Since expressjs 4.0 the app is ran using npm start command instead of node app.js.
I tried to configure a script:
{
"name": "feed-stats",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "nodemon app.js",
// or ...
"start": "nodemon ./bin/www"
},
"dependencies": {
"express": "~4.0.0",
"static-favicon": "~1.0.0",
"morgan": "~1.0.0",
"cookie-parser": "~1.0.1",
"body-parser": "~1.0.0",
"debug": "~0.7.4",
"jade": "~1.3.0",
"mongodb": "*",
"monk": "*"
}
but I can't get it work.
Can you help me ?
Thanks,
Cause Express 4.x separates the start server module into ' ./bin/www ' file, you should put following codes (default in ' ./bin/www ') into 'app.js', so that you can start your app with node app.js and also nodemon app.js.
// no need 'var app = require('../app');' any more, cause it has defined in 'app.js' already.
var debug = require('debug')('my-application');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
// module.exports = app; You need to comment this line which is default in 'app.js' with Express.js 4.x
You can remove these codes and the comment of the last line, and you will be able to start your app with default way later.
I've just tried this code, and it doesn't work (without using nodemon).
You've got comments like "no need for var app = express()" - but I don't understand why. Equally, you're requiring in my-application which I don't have so I can't test.
If I make a couple of changes to the package.json and the app.js nodemon runs the express4 app just fine:
app.js:
var app = require('express')();
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);
});
module.exports = app;
package.json:
{
"name": "feed-stats",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "nodemon ./bin/www/app.js"
},
"dependencies": {
"express": "~4.0.0",
"static-favicon": "~1.0.0",
"morgan": "~1.0.0",
"cookie-parser": "~1.0.1",
"body-parser": "~1.0.0",
"debug": "~0.7.4",
"jade": "~1.3.0",
"mongodb": "*",
"monk": "*"
}
}
But I feel like there's something you're not including - because you're not showing me the full example (or output).
My gut feeling is that you should actually just correct your package.json's start value to nodemon ./bin/www/app.js and that should do the trick.
I've corrected the error.
It should be var app = require('../app'); that we don't need it.
Cause var app = express(); exists in 'app.js' already.
So, is this a nodemon error or was it an error with your code? (sorry, again, an little unsure).
It's my fault, so sorry.
works for me, thank you
ps: maybe this solution may be reported in nodemo documentation ?
@mickaelandrieu I'm not sure its actually a nodemon issue though. I got the impression this was just an implementation issue in the user code.
I encountered the same issue. Resolved as follows:
Added to MyProject/app.js
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);
});
Adjusted script start path in MyProject/package.json
...
"scripts": {
"start": "nodemon ./app.js"
},
...
$ npm start
. . .
> nodemon ./app
25 Apr 16:33:48 - [nodemon] v1.0.17
25 Apr 16:33:48 - [nodemon] to restart at any time, enter `rs`
25 Apr 16:33:48 - [nodemon] watching: *.*
25 Apr 16:33:48 - [nodemon] starting `node ./app`
Express server listening on port 3000
I'm new to node and suspect the above is a bit of a hack since it bypasses /bin. I also don't understand pointing to ./bin/www/app.js in the suggested solution above. I thought app.js lives in the root of the project, not in /bin.
Had same issue. It's the new file structure in express 4.0.
Easy, three-step fix:
1) Create a new file (e.g. start.js) in your project's root folder
2) Add require('./bin/www') to your new file and save file
3) Change start script in package.json to execute nodemon on new file
{
"name": "happy blog",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "nodemon start.js" // <= HERE!!!!
},
"dependencies": {
"express": "~4.2.0",
. . .
}
}
This method saves the hassle of copying code from ./bin/www, pasting said code into ./app.js, and then refactoring all code to work together. Also, simply running nodemon ./app.js will drop the vital code in ./bin/www from your application. The method above works and is much cleaner .
I want to resolve this issue properly (and I have an idea /how/ to), but I really don't know how people are getting this ./bin/www directory.
It might be because I'm still using express 3 for production, but looking at the express docs, I can't find reference to this either.
@deluxicon can you explain how you're creating your _default_ express projects so I can try to repliate the issue that people are seeing?
Ah, I think I've found it - express-generator. Giving it a try now.
Hi @deluxicon @arwhyte and @mickaelandrieu -
I'm trying to get to the root of this issue and seeing if nodemon needs to be smarter about what it does, but I'm having trouble actually replicating any issues.
When I generate the project using the express generator, it ends with:
run the app:
$ DEBUG=express4-gen ./bin/www
...and if I put nodemon in there:
$ DEBUG=express4-gen nodemon ./bin/www
It runs fine (and reloads, etc). Then if I change the npm start value from:
"start": "node ./bin/www"
To:
"start": "nodemon ./bin/www"
Also works.
So is the _actual_ problem when you try to simply run:
$ nodemon
nodemon returns and says:
Usage: nodemon [nodemon options] [script.js] [args]
See "nodemon --help" for more.
Is that it?
I'll take a look soon :)
Thanks for help !
@mickaelandrieu I'm going to release nodemon 1.1.0 that will also search inside the package.json file for the start property. If it finds it, it'll kick off the process as if you did npm start but with nodemon watching in place. Does that sound like what you needed?
Yes, sounds good.
That's live now with 1.1.x
@remy
I have been using express-generator to create new projects. The actual npm start problem may be beyond my understanding... may be an OS issue since (for me at least) your method works great on Windows but was not working on Mac. Not absolutely sure, though.
That said, I understood the problem enough to just add the start.js file and run with it. My method may just be one of many good backup plans.
Thanks for looking into the problem. Modules like nodemon are true time-savers.
@deluxicon, your 3 steps work.
I just run nodemon start.js. The application runs.
Thanks.
+1 for the 3-step fix.
Worked great here.
Thanks @openwonk 's three steps.
I finally got the answer.
I use
nodemon ./bin/www -w ./
at my project directory and it works.
I using express4.0 and nodemon 1.11.0.
When I run "node app.js" then it work well.
But When I am trying to use gulp + nodemon to run it. There are nothing found from my browser no matter which port I specified.
Here is my code:
// Sniff out if there have changes in back-end.
gulp.task('develop', function() {
var stream = $.nodemon({
script: 'api/app.js',
watch: ['api'],
ext: 'js',
ignore: [''],
tasks: ['']
})
stream
.on('restart', function() {
console.log('restarted!')
})
.on('crash', function() {
console.error('Application has crashed!\n')
stream.emit('restart', 6) // restart the server in 10 seconds
})
});
app.listen(3000, function() {
console.log('http://127.0.0.1:3000/ Get rolling successfully');
});
Some guys can help me on this? Thanks
I have an issue to with this. But sometimes it's okay and I still can't find out why...
I have a ./bin/www server as express-generator propose.
npm start is equal to nodemon -w ./ ./bin/www
I have a message that it's under the eye.
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /Users/Vadim/myproject/**/*
[nodemon] starting `node ./bin/www`
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
So this is issue is still in business for me, because nodemon is watching files normally as I suppose but not triggering reload with code change
@bearzx
It works for me but could you tell me what the magic is behind "-w ./" ?
Use -e hbs,js to watch both JavaScript and hbs files
On Fri, 19 Apr 2019, 21:08 chrisjaimesdev, notifications@github.com wrote:
is there a way I can make nodemon listen to changes to my hbs files by
using nodemon ./bin/www ??—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/remy/nodemon/issues/330#issuecomment-485001964, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAADLBCHM57UPIMAUXB6GMLPRIRFFANCNFSM4AOR5OJA
.
is saw this one from video
"start": "if [[ $NODE_ENV == 'production' ]]; then node ./bin/www; else nodemon ./bin/www; fi"
"scripts": {
"start": "nodemon ./bin/www"
},
This works for me!
Most helpful comment
I use
at my project directory and it works.