The Node.js 8 Runtime

The Cloud Functions Node.js 8 runtime is based on Node.js version 8.11.1. For instructions on how to 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 Node.js 8 runtime for your function during deployment.


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 nodejs8 --trigger-http

For more arguments that you can specify when you are deploying, see Deploy using the gcloud tool.


If you are using the GCP Console, you can select the runtime when you create and deploy a function.

  1. In the GCP 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.

  2. Click Create Function.

  3. Under Runtime, select Node.js 8.

Execution environment

The execution environment includes the runtime, the operating system, packages, and a library that invokes your function.

Base image

For the Node.js 8 runtime, Cloud Functions uses an execution environment based on Ubuntu 18.04. Updates to the language runtime are done automatically (unless otherwise notified), and include changes in the definition of the base image.

HTTP library

The Node.js 8 runtime uses ExpressJS to handle incoming requests. The Node.js 8 runtime currently uses ExpressJS version 4.16.3. This helper invokes your function in response to an incoming request and takes care of other details, such as handling HTTP.

Updates to the execution environment

This runtime receives automatic updates to the version of Node.js. For example, Cloud Functions currently uses Node.js version 8.11.1. When a new version of Node.js 8 is released to the language community, the runtime version might be updated.

Similarly, Cloud Functions might apply updates to other aspects of the execution environment, such as the operating system, packages, and other elements of the environment. These updates help keep your function secure.

Source code structure

Your function's source code must be exported from a Node.js module, which Cloud Functions loads using a require() call. In order to determine the module to load, Cloud Functions uses the main field in your package.json file. If the main field is not specified, Cloud Functions loads code from index.js.

For example, the following configurations of source code are valid:

  • A single index.js file that exports one or more functions.
  • An index.js file that imports code from a foo.js file and then exports one or more functions.
  • An app.js file that exports one or more functions, with a package.json file that contains "main": "app.js".

Any configuration works as long the require() call can load your function.

Specifying dependencies

A function is allowed to use external Node.js modules as well as local data. Dependencies in Node.js are managed with npm and expressed in a metadata file called package.json.

To specify a dependency for your function, add it to your package.json file. If you are deploying through the gcloud command-line tool, you can also pre-install dependencies and deploy them alongside your function. By default, the node_modules folder is added to your .gcloudignore file and is not uploaded as part of your deployment. To deploy pre-installed dependencies, remove node_modules/ from the .gcloudignore file before deploying your function.

After you deploy your function, Cloud Functions installs dependencies declared in the package.json file using the npm install command, or if a yarn.lock file exists, the yarn install command.

In this example, a dependency is listed in the package.json file:

  "dependencies": {
    "uuid": "^3.0.1"

The dependency is then imported in the function:

const uuid = require('uuid');

// Return a newly generated UUID in the HTTP response.
exports.getUuid = (req, res) => {

Using npm to install Node.js modules

The easiest way to install a Node.js module is to use the npm install command in the folder containing your Cloud Function. For instance, the following command adds the uuid module:

npm install uuid

This combines two steps:

  1. It marks the latest version of the module as a dependency in your package.json file. This is very important: Cloud Functions only installs modules that are declared in your package.json file.
  2. It downloads the module into your node_modules directory. This lets you use the module when developing locally.

If you don't have npm installed on your machine, get npm.

Loading Node.js modules

Use the Node.js require() function to load any Node.js module you have installed. You can also use the require() function to import local files you deploy alongside your function.

Using private modules

In order to use a private npm module, you must provide credentials (auth token) for the npm registry in a .npmrc file located in the function's directory. The npm documentation explains how to create custom read-only access tokens. We discourage using the .npmrc file created in the home directory because it contains a read-write token. Write permissions are not required during deployment, and could pose a security risk.

Do not include the .npmrc file if you're not using private repositories, as it can increase the deployment time for your functions.


Node.js 8.x is the first LTS version of Node.js to support the async and await keywords. The following example shows a simple function that uses these new keywords:

 * Background Cloud Function to be triggered by Pub/Sub.
 * This function is exported by index.js, and executed when
 * the trigger topic receives a message.
 * @param {object} data The event payload.
 * @param {object} context The event metadata.
exports.helloPubSub = (data, context) => {
  const pubSubMessage = data;
  const name = ? Buffer.from(, 'base64').toString() : 'World';

  console.log(`Hello, ${name}!`);

When this function is triggered by a message on a Cloud Pub/Sub topic, it extracts the topic name from the context object, gets metadata associated with the Cloud Pub/Sub topic, then prints that metadata to Stackdriver Logging. The function uses the await keyword to turn an asynchronous metadata request into a synchronous operation.

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.

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Functions Documentation