I come across ngrok more and more every day; it provides an excellent HTTP tunnel from my local computer to the internet. I use it to debug Slack apps locally, so I would like to integrate it with my Node.js application.

The free version generates a new sub domain every time you connect, that’s why I’m usually start ngrok at the beginning of my day. I’ve tried to use the ngrok npm package in my application, but as the documentation says: “The ngrok and all tunnels will be killed when node process is done.” I need the process to “survive” my application. Let’s see what we can do about that…

The ngrok package ships the ngrok binary, which will launch the ngrok tunnel. The axios package is used to retrieve the ngrok URL. The fkill package allows us to stop expired tunnels by killing the ngrok process.

Ngrok is a wonderful tool to provide HTTP tunnels for local development. I’m looking forward to use it more and more in open source projects. A big shout-out to Arno Jansen for testing and debugging the code in our Slack bots. We’re working on version II of bot-zero, which will have this code out of the box.

Improvements2020-02-08: only require dev packages when they are absolutely needed.2020-02-08: environment variables might be loaded in a later stage, so only look for the NGROK variable when needed.2020-02-08: expired tunnels will be killed using fkill.2020-02-09: added ngrok kill command for Mac and Linux.