It seems the local server started with "heroku local web" does not watch for file changes and restart itself. How can I make it do this?
The easiest way to do this is to run nodemon with heroku local
as the executable - i.e. nodemon --exec "heroku local"
.
However, heroku local
exits with a non-zero exit code for the default nodemon shutdown signal (SIGUSR2), so you need to add an additional flag to nodemon to set the interrupt signal to SIGTERM.
nodemon --exec "heroku local" --signal SIGTERM
(tested with [email protected], [email protected], [email protected])
heroku local
just uses node-foreman
(https://www.npmjs.com/package/heroku-local), so it is easier to use that directly for watching.
First, install foreman
and nodemon
:
npm i --save-dev foreman nodemon
Now, you need to set up two scripts in your package.json
:
{
...
"scripts": {
"start": "nf start",
"watch": "nodemon --watch directory-to-watch"
},
...
}
You can now run the app while it watches that directory and reloads on changes with
npm run watch
I think that heroku local
will watch
for changes to static resources (client-side code). But, it's clearly not ideal to manually restart your server with each source code change you make to the web server (server-side code).
If you're creating a NodeJS application, my suggestion is to try the watch
command that Gulp provides. However, that also requires you to (a) install Gulp, and (b) write the Gulp script. Again this solution would only work for NodeJS, and these steps wouldn't be needed if heroku local
watched the files for you.
- Install NodeJS
Install Gulp
npm install gulp --save-dev
Add the Gulp script (where Procfile lives)
NOTE: Haven't been able to get the following
watch
code to work successfully, but it might provide a template for others to create a working solution.var gulp = require('gulp'); var exec = require('child_process').exec; gulp.task('heroku-local', function () { exec("heroku local"); }); gulp.task('heroku-local:watch', function () { gulp.watch([ 'file-to-watch', 'folder-to-watch/**/*' ], ['heroku-local']); });
Run the Gulp script (from directory where Procfile lives)
gulp heroku-local:watch
spawn
method? nodejs.org/api/… That might work... –
Ragsdale /killme
, and then starting it again. Of course this url should not exist on the live website, but is useful locally. in the gulpfile: exec("curl http://localhost:5000/killme ; heroku local");
in my node.js/express app: router.get('/killme', function(req,res) { process.exit(1); });
–
Forthwith For anybody coming across this post in search for a solution... If you are ok with installing nodemon globally on your machine, you can update the heroku Procfile (https://devcenter.heroku.com/articles/procfile) to use nodemon instead of node:
web: nodemon index.js
Again, nodemon must be installed globally for this to work:
npm i -g nodemon
Then you should be able to run heroku locally as normal with nodemon watching for changes:
heroku local web
(creating answer as I can't reply yet)
complementing https://stackoverflow.com/users/12663699/nick-ruiz answer,
you don't need to install nodemon globally.
You can have it as a dev dependency
npm i nodemon --save-dev
and then, create a Procfile in your project root with the path to nodemon inside node_modules.
web: node index.js
dev: ./node_modules/nodemon/bin/nodemon.js index.js
This way you can run heroku local
and have it restart on changes.
(this is meant for users who want to run node index.js
on the container and nodemon index.js
in their local machine)
© 2022 - 2024 — McMap. All rights reserved.
heroku local
with any changes. – Tout