The Node.js 6 Runtime

The Cloud Functions Node.js 6 runtime is based on Node.js version 6.14.0. For instructions on how to how to prepare your local machine for Node.js development, see Setting Up a Node.js Development Environment.

Selecting the runtime

The Node.js 6 runtime is generally available and is the default runtime selected when you deploy your function.


If you are using the gcloud command-line tool, the Node.js 6 runtime is selected by default. For example:

gcloud functions deploy NAME --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 6.

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 6 runtime, Cloud Functions uses an execution environment based on Debian 8. 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 6 runtime uses ExpressJS to handle incoming requests. 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 6.14.0. When a new version of Node.js 6 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.

Cloud Functions file naming

When creating a function, your source code must be exported in a Node.js module. The module is loaded using a require() call, so functions are typically contained within a simple index.js file.

The following example configurations are permissible, but other configurations might work as well:

  • An index.js file that 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".
  • An index.js file that imports one or more functions from a foo.js file and then exports one or more functions.

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

Specifying dependencies

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

If you are deploying through gcloud, Cloud Functions downloads dependencies declared in package.json using npm. You can also prepackage fully materialized dependencies alongside your function. By default, node_modules is added to .gcloudignore and is not uploaded as part of your deployment.

If you are deploying through the Firebase CLI, the local node_modules folder is ignored, and you must specify your dependencies in package.json. You can learn more about the package.json file from the npm docs.

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.

Loading Node.js modules

Just as you can depend on third-party libraries like the uuid module, you can use require (JavaScript) to load any Node.js module you have installed. The require function takes the name of the Node.js module and returns that module's exports object.

Using private modules

In order to use a private npm module, you have to provide credentials (auth token) for the npm registry in a .npmrc file located in the function's directory. You can simply copy the .npmrc file that was created in your home directory when you logged into npm using the npm login command.

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

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.

Terminating background functions

By default, background functions expect you to invoke a callback to signal the completion of the function. However, dealing with external libraries that return Promises can be cumbersome, so we also allow background functions to optionally return a Promise rather than explicitly invoke a callback. When returning a Promise, remember to omit the callback argument from the method signature.

 * Background Cloud Function that returns a Promise. Note that we don't pass
 * a "callback" argument to the function.
 * @param {object} event The Cloud Functions event.
 * @param {object} The event data.
 * @returns {Promise}
exports.helloPromise = (event) => {
  const request = require('request-promise');

  return request({

You can alternatively return discrete values from the function, and Cloud Functions will handle the callback. This is particularly useful if your function does not have any asynchronous calls, or there are conditions under which you can return a synchronous response. When you use this approach, a return value is considered a successful outcome, but you can also throw an error to signal a failure outcome.

For example:

 * Background Cloud Function that returns synchronously. Note that we don't pass
 * a "callback" argument to the function.
 * @param {object} event The Cloud Functions event.
 * @param {object} The event data.
exports.helloSynchronous = (event) => {
  // This function returns synchronously
  if ( === true) {
    return 'Something is true!';
  } else {
    throw new Error('Something was not true!');
Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Functions Documentation