TORIPIYO DIARY

recent events, IoT, programming, security topics

Restart nodejs application on file change or error output in "heroku local" command

This article is English translation of http://toripiyo.hatenablog.com/entry/2018/03/11/185532 Japanese article.

I am deploying nodejs application on heroku platform as production. In local environment I use "heroku local" command to run application on local machine.
I googled to find a way to make nodejs application automatically restart on file change or error message output by "heroku local" command.
However I couldn't find a proper article to explain it. So I would like to summarize it as note to myself.

By following below steps, the application can automatically restart on file change or stop by error message output.

0. install forever

npm install forever -g

1. insert below line on Procfile which located on application's root directory

development: forever -w -m 500 -c "node --inspect=5858" app.js
  • -w option is used to restart application on file change
  • -m option is used to specify how many times restart application (in this example the application can be restarted 500 times if error output stops the application)
  • -c option is used to specify the command which executed when "heroku local development" command is run. --inspect option is used to open 5858 port for debug purpose.

2. put .foreverignore file on application root directory

(.foreverignore)
=====================================
**/.idea/**
**/.git/**

**/.tmp/**
**/views/**
**/assets/**

# Restart only when js files are changed.
!*.js
=====================================
  • notice: .foreverignore writing style is different from .gitignore style. the application can decide to restart by referring to .foreverignore contents.

foreverignore syntax/documentation · Issue #417 · foreverjs/forever · GitHub


3. run application with "heroku local" command

heroku local development

By following above steps, the application which managed by "heroku local" command can automatically restart even on file change or stop by error message output.