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?

Damjan Pavlica
  • 31,277
  • 10
  • 71
  • 76
Joren
  • 9,623
  • 19
  • 63
  • 104

5 Answers5

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 heroku-cli@6.14.31-33a2d0a, nodemon@1.12.1, node@8.5.0)

lennym
  • 239
  • 2
  • 2
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
Spain Train
  • 5,890
  • 2
  • 23
  • 29
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
    
tim-montague
  • 16,217
  • 5
  • 62
  • 51
  • Have you tried the `spawn` method? https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options That might work... – tim-montague Feb 16 '16 at 17:41
  • 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); });``` – Lucas Apr 21 '16 at 22:38
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

Nick Ruiz
  • 21
  • 1
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)

glb
  • 1