Hot Reload Node Cloud Functions

Grant Timmerman
Google Cloud - Community
2 min readDec 20, 2021

The Google Cloud Function Framework for Node allows for you to develop serverless functions locally on your computer by spinning up a web server in the same way that is run on Google Cloud Functions. As you likely know, local development greatly increases the velocity of development, rather than having to wait 1–2 minutes for your function to deploy.

However, a common developer problem is the fact that — once your function is running with the Functions Framework, any updates to your source code aren’t reflected in the running HTTP server.

In this blogpost, I’ll give you a quick overview for how you can add hot reloading to your Node Cloud Function developer workflow.

Here’s a picture of me hot-reloading break with a latte in Puerto Natales, CL. I have never said “no” to another latte while coding.

The Node Functions Framework is a lightweight library that wraps around the popular and familiar TJ Holowaychuk’s Express.js framework. The framework allows you to write a simple server that invokes your function upon receiving a HTTP request:

https://github.com/GoogleCloudPlatform/functions-framework-nodejs/

Running a Simple Cloud Function

Here’s a simple function at index.js:

And here’s a minimal package.json:

To test this function:

  • Run npm i to install the Functions Framework
  • Run npm start to start the Functions Framework

You can then go to http://localhost:8080/ and see the function is invoked with the response: Hot-reload me?.

However, if you then go and change 'Hot-reload me?' to 'Why isn't it working???¿?' and refresh the page, you will see the old response still. :(

This result is due to the fact the Node program only loads your function once when starting the framework (with npm start) and starting a web server.

How can we fix this?

Introducing npm Watch

Ideally, we would like to automatically restart the Functions Framework whenever our source code changes.

To achieve that, we can add the handy npm-watch package:

And with that, we can just run npm run watch to re-run the start command upon any changes to *.js files:

Reloading the function via nodemon

Thanks for reading!

If you enjoyed this, be sure to give a clap 👏, or write a comment ✏️ on that may be featured on a future post!). I don’t write that many blogposts anymore, but let me know if they’re enjoyable and I may change that :)

Thanks to Preston Holmes for the GitHub issue and the open source community for the great discussion: https://github.com/GoogleCloudPlatform/functions-framework-nodejs/issues/24

--

--