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.

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:

Sample output from Google Cloud Function using Node 8 Features

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

And that’s all there is to it!