Handling 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.

Note: Deploying pre-installed dependencies works with gcloud only; the
Firebase CLI ignores the local node_modules folder.

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 @types/uuid

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

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

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.

If you are writing functions in TypeScript, use the
import
statement in the same way to load any Node.js module you have installed.

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.