Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Configure ngrok with nodemon for local development
#!/usr/bin/env node
if (process.env.NODE_ENV === 'production') {
throw new Error("Do not use nodemon in production, run bin/www.js directly instead");
}
const nodemon = require('nodemon');
const ngrok = require('ngrok');
// We start an ngrok tunnel to ensure it stays the same for the entire process
ngrok.connect({
proto: 'http',
addr: '3001'
}, (err, url) => {
if (err) {
console.error("Error opening ngrok tunnel", err);
process.exit(1);
} else {
// We inject the url as NGROK_URL env var into our node process,
// and have nodemon start our main web server process
clonsole.log("Ngrok tunnel opened at " + url);
nodemon(`-x 'NGROK_URL=${url} node' ./bin/www.js`);
}
});
nodemon.on('start', function () {
console.log('App has started');
}).on('quit', function () {
console.log('App has quit');
}).on('restart', function (files) {
console.log('App restarted due to: ', files);
});
@spalladino

This comment has been minimized.

Copy link
Owner Author

@spalladino spalladino commented Oct 15, 2017

This snippet starts an ngrok tunnel, and then runs nodemon with the main node process. This ensures that the generated ngrok URL stays the same during the entire development session, while nodemon reloads the app server as needed.

@ZarifS

This comment has been minimized.

Copy link

@ZarifS ZarifS commented Nov 17, 2019

Where do i place this in my express setup? right now my app.js is the where the server gets started and i am using nodemon script form package.json to watch the server

@spalladino

This comment has been minimized.

Copy link
Owner Author

@spalladino spalladino commented Nov 19, 2019

Where do i place this in my express setup? right now my app.js is the where the server gets started and i am using nodemon script form package.json to watch the server

@ZarifS it has been a while since I wrote this script, but you should place this at the root of your project, and ensure that app.js is listed as the main file of your package.json so nodemon picks it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment