Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I have foreman reload node.js when a file is changed?

I currently use supervisor to run my node.js app when developing locally. This has been great, but I currently store all my configs in my .bash_profile, I want to move them to a .env file.

Is there a way to get the environment loading features of foreman with the file watching features of supervisor?

One option is to add it to my Procfile like this, but I suspect that will mess up Heroku.

`local: supervisor web.js`
like image 490
Shane Stillwell Avatar asked Dec 02 '22 20:12

Shane Stillwell


2 Answers

This is the solution I put together and it works great.

Install Rerun and Rb-fsevent.

sudo gem install rerun rb-fsevent  

Install Foreman if you have not already.

sudo gem install foreman

Put your environment variables in .env in the root of your project.

Don't forget to add .env to your .gitignore, don't want that sensitive info in your code

echo '.env' >> .gitignore  

Here is what my Procfile looks like

web: node web.js

Now just start like this

rerun foreman start web  

Extra Credit, create an alias

echo "alias rrun='rerun foreman start web'" >> ~/.bash_profile  
like image 140
Shane Stillwell Avatar answered Dec 23 '22 05:12

Shane Stillwell


Another solution is to create another Procfile file for your development environment with your current configuration:

# Procfile.dev
web: supervisor app.js

And start foreman using that file

$ foreman start -f Procfile.dev

You can optionally exclude that file from your git repo

$ echo "Procfile.dev" >> .git/info/exclude
like image 26
Diego Pino Avatar answered Dec 23 '22 04:12

Diego Pino