Handling 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 shipped alongside your function. If you are deploying through gcloud, you can either prepackage fully materialized dependencies within your function package or declare them in the package.json file, and Cloud Functions will download them for you when you deploy. 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:

JavaScript

const uuid = require('uuid');

// Return a newly generated UUID in the HTTP response.
exports.getUuid = functions.https.onRequest((req, res) => {
  res.send(uuid.v4());
});

TypeScript

import * as uuid from 'uuid';

// Return a newly generated UUID in the HTTP response.
export let getUuid = functions.https.onRequest((req, res) => {
  res.send(uuid.v4());
}

Using npm to install Node.js modules

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

npm install --save 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.

Additional steps for TypeScript

TypeScript helps you most when you use libraries that have type information. This lets TypeScript catch syntax errors and lets editors give you better autocomplete suggestions. Some libraries, like firebase-admin and firebase-functions, ship with TypeScript definitions included.

Many libraries do not provide their own TypeScript definition. The DefinitelyTyped project provides community-maintained definitions for the most popular node libraries. DefinitelyTyped publishes these definitions under the same NPM package name, but inside the "@types" organization. For example, you can install the type information for the uuid library with the following:

npm install --save @types/uuid

As you become more familiar with TypeScript, you may find yourself combining both installs:

npm install --save uuid @types/uuid

Type dependencies should be the same kind as the library dependency. For example, you should not save uuid as a normal dependency and @types/uuid as a dev dependency or peer dependency.

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.

If you are writing functions in TypeScript, use import in the same way to load any Node.js module you have installed. The import 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 may increase the deployment time for your functions.

Send feedback about...

Need help? Visit our support page.