cloud, gcp, node.js, javascript, serverless, coding,

You Can Now Write Google Cloud Functions with Node.js 8

Simon Prickett Simon Prickett 3 mins read
You Can Now Write Google Cloud Functions with Node.js 8

In my previous article looking at environment variables with Google Cloud Functions I noted that one of the downsides of using them is the relatively old Node.js 6 runtime that Google provides. As the environment is fully managed there was no option to use a newer Node.js version. Things changed recently, as Google has now added beta runtimes for Node 8 and Python 3. This is a welcome move towards catching up on Amazon’s lead in this space — AWS Lambda has had these features for a while (and supports other languages too). The old Node.js 6 runtime remains the default and will be used unless you explicitly specify that you want Node.js 8 when deploying your functions.

Let’s take a look at how to use the Node.js 8 environment by deploying a function that uses a couple of features that weren’t available in Node.js 6. The specific version of Node.js 8 that Google is currently using is 8.11.1.

Probably the most significant change between Node.js 6 and 8 is the introduction of support for the async and await keywords (see MDN for an explanation of these). Node.js 8 also adds other useful items such as object spread syntax.

Enjoying this article? Please consider buying me a coffee on Ko-Fi.
Your support helps to fund future projects!

Let’s try out the Node.js 8 runtime with a simple HTTP function that uses async, await and the object spread syntax to get some data from a public API, amending it slightly before returning it to the caller. We’ll use the node-fetch module, adding the ability to use the same syntax as the window.fetch API that the browsers have:

The function uses node-fetch to grab an object representing a single randomly generated user from the excellent randomuser.me API. We then add an extra property generator using the object spread syntax and return the result to the caller as JSON.

When deploying this function we have to tell Google to use the Node.js 8 runtime, as we’re using syntax not available in Node.js 6. To do this we’ll need to make sure we have the latest gcloud beta commands:

$ gcloud components update
$ gcloud components install beta

Deployment is pretty simple (note we have to use the beta commands and explicitly say that we want the Node.js 8 runtime):

$ gcloud beta functions deploy getUserDetails --runtime nodejs8 --trigger-http --project <projectId>

Where <projectId> is the ID of your Google Cloud project. The function can be invoked simply by visiting its URL which will look something like:

https://<region>-<projectId>.cloudfunctions.net/getUserDetails

<region> and <projectId> will depend on your Google Cloud project setup. The output looks like this:

Function output
Function output.

The Cloud Functions console also shows that the function is using the Node.js 8 runtime:

Cloud Functions console
Google Cloud Functions Console.

And that’s all there is to it!


If you’d like to use or study the code from this article feel free: I’ve put it on GitHub for you. Google’s documentation for the Node.js 8 Cloud Functions runtime can be read here — this includes important information about a signature change for background functions that use the Node.js 8 environment. If you’re interested in tracking which features became available in which Node release, I recommend node.green as an excellent resource.

Thanks for reading!

Simon Prickett
Written by Simon Prickett
Hugely Experienced Developer Relations Leader.