Migrating Cloud Functions to newer Node.js runtimes

The Node.js 6 runtime has been deprecated. The Node.js 8 runtime will be deprecated on 2020-06-05. To ensure that your functions are on a supported version of Node.js, migrate them to Node.js 10. After 2020-08-05, function deployments that use Node.js 6 will be blocked. Cloud Functions that continue to use Node.js 6 after this time may be disabled.

The Node.js 10 runtime introduced changes that might require you to modify your function's source code to use the newer runtimes. This page describes the changes and provides guidelines for migrating existing functions to newer runtimes.

Differences in the Node.js 10 runtime

For the majority of applications, upgrading from Node 8 to Node 10 will involve no code changes (the major difference being changes in environment variables on GCF).

Node 10 introduces a variety of exciting new features, such as async iterators and a promise-based file API, along with performance improvements and security patches.

Signature changes after Node.js 6

The signature for background functions changed between the Node.js 6 and the Node.js 8 runtimes, continuing into Node.js 10.

An example Node.js 6 background function might look as follows:

exports.nodejs6BackgroundFunction = (event, callback) => {
  let data = event.data;
  let context = event.context;
  // ... the rest of your function

Note that the event argument contains data and context properties. In the Node.js 10 runtime, the data and context properties have been extracted from the event object and are now included as part of the function signature:

exports.nodejs8AndAboveBackgroundFunction = (data, context, callback) => {
  // ... the rest of your function is unchanged

This change simplifies references to the data and context objects previously contained within the event argument. For more information, see Background function parameters.

If you are using the Node.js 6 background function signature, make the following changes to run your function in a newer Node.js runtime:

  1. Change the function signature from (event, callback) to (data, context, callback).

  2. Change references to event.data and event.context to data and context, respectively.

Environment variable changes

Node.js 10 runtime introduced changes to the environment variables that are predefined by the runtime. See Environment variables set automatically for a full description of the differences between environment variables set by the Node.js 10 runtime versus previous runtimes.

To run your older Node.js functions in the Node.js 10 runtime, you might need to make changes to any predefined environment variables you are using. Update your code to use the replacement variables where possible or set them yourself when deploying your function. As a best practice, we recommend that you do not depend on environment variables that you have not set explicitly.

The following environment variables are no longer set by the Node.js 10 runtime:

Environment variable Migration
CODE_LOCATION Set it when deploying your function. Your code location is /srv.
ENTRY_POINT Use the FUNCTION_TARGET environment variable instead.
GOOGLE_CLOUD_PROJECT Set it when deploying your function.
GCP_PROJECT Set it when deploying your function.
GCLOUD_PROJECT Set it when deploying your function.
GOOGLE_CLOUD_REGION Set it when deploying your function.
FUNCTION_REGION Set it when deploying your function.
FUNCTION_NAME Use the K_SERVICE environment variable instead.
FUNCTION_IDENTITY Set it when deploying your function.
FUNCTION_MEMORY_MB Set it when deploying your function.
FUNCTION_TIMEOUT_SEC Set it when deploying your function.
FUNCTION_TRIGGER_TYPE Use the FUNCTION_SIGNATURE_TYPE environment variable instead. See Using environment variables for usage details.
OLDPWD No longer available.
SHLVL No longer available.