Restart Heroku local on file change?
Asked Answered
A

5

23

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?

Audiovisual answered 8/12, 2015 at 1:2 Comment(0)
S
23

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])

Salol answered 4/10, 2017 at 9:11 Comment(3)
This works as desired - restarting heroku local with any changes.Tout
Works great! Thanks!Assurgent
Did not work with nodemon 1.11.0 but upgraded to 1.18.3 and workedBarrelhouse
P
6

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
Pigskin answered 22/2, 2017 at 6:30 Comment(0)
R
1

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.

  1. Install NodeJS
  2. Install Gulp

    npm install gulp --save-dev
    
  3. 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']);
    });
    
  4. Run the Gulp script (from directory where Procfile lives)

     gulp heroku-local:watch
    
Ragsdale answered 14/2, 2016 at 2:43 Comment(2)
Have you tried the spawn method? nodejs.org/api/… That might work...Ragsdale
I'm using the gulp watcher to terminate node process by visiting a special route I've created, /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
O
1

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

Ormandy answered 6/1, 2020 at 19:36 Comment(0)
E
0

(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)

Expect answered 15/2, 2022 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.