zesty.io

Product

Use Cases

Integrations

Learn

Not Categorized

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.

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://&lt;region&gt;-&lt;projectId&gt;.cloudfunctions.net/getUserDetails

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

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

And that&rsquo;s all there is to it!

--

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

This article first appeared on Simon's Medium. Feel free to subscribe to Mindshare to see more of his work, or follow him directly on Medium!

By Simon Prickett

Software professional, specializing in IoT, Node, mobile web & apps. Atheist, electronics hobbyist. Also aviation, cars & hockey.

Related Articles

Subscribe to the zestiest newsletter in the industry

Get the latest from the Zesty team, from whitepapers to product updates.