Cloud Functions supports the following Node.js runtimes:
- Node.js 10
- Node.js 12
- Node.js 14 (public preview)
For instructions on how to prepare your local machine for Node.js development, see Setting Up a Node.js Development Environment.
To get started with Node.js on Cloud Functions, see the Quickstart.
Selecting the runtime
You can select the desired Node.js runtime for your function during deployment.
gcloud
If you are using the gcloud
command-line tool, you can specify the runtime
by using the --runtime
parameter. For example:
gcloud functions deploy NAME --runtime nodejs12 --trigger-http
For more arguments that you can specify when you are deploying, see Deploy using the gcloud tool.
Console
If you are using the Cloud Console, you can select the runtime when you create and deploy a function.
In the Cloud Console, go to the Cloud Functions Overview page.
Go to the Cloud Functions Overview page
Make sure that the project for which you enabled Cloud Functions is selected.
Click Create Function.
Under Runtime, select a Node.js runtime, for example, Node.js 10.
Execution environment
The execution environment includes the runtime, the operating system, packages, and a library that invokes your function.
The Node.js 10+ runtimes use an execution environment based on Ubuntu 18.04 with Node.js version 10.18.1. See Cloud Functions Execution Environment for more information.
The library that invokes your function is the Node Functions Framework.
Source code structure
In order for Cloud Functions to find your function's definition, each runtime has certain structuring requirements for your source code. See Writing Cloud Functions for more information.
Specifying dependencies
You can specify dependencies for your functions by listing them in a
package.json
file. For more information, see
Specifying dependencies in Node.js.
Environment variables
The Node.js 10+ runtimes automatically set fewer environment variables than previous runtimes supported by Cloud Functions. For details, see Using Environment Variables.
Using middleware to handle HTTP requests
Node.js HTTP Cloud Functions provide request
and response
objects
that are compatible with
ExpressJS
to make consuming HTTP requests simple. Cloud Functions automatically reads the
request body, so you will always receive the body of a request independent of
the content type. This means that HTTP requests should be considered to have
been fully read by the time your code is executed. The nesting of ExpressJS
apps should be used with this caveat—specifically, middleware that expects the
body of a request to be unread might not behave as expected.
Node.js 14 (public preview)
Node.js 14 (public preview) introduces some new features and concepts. Highlights:
- Optional chaining.
Optional chaining looks like this:
{"hello": null}?.hello?.neat
. It allows safe access to deep keys on objects that may not exist. - Nullish coalescing. This
introduces
??
which is safer than using||
for assignment (as it only evaluates tofalse
fornull
orundefined
).
You can learn more about Node.js 14 features here.