You specify the dependencies for your Node.js app by declaring
them in the package.json file.
For example, if you want to specify Lodash
as a dependency, your package.json file might look as follows:
{
"dependencies": {
"lodash": "^4.0.1"
}
}
During deployment, the Node.js runtime
automatically installs
all dependencies declared in your package.json file. By default, the
npm install command is used, however Yarn and Pnpm package managers are also
supported:
Yarn: If a
yarn.lockfile exists, theyarn install --productioncommand is used instead.Pnpm: If a
pnpm-lock.yamlfile exists, thepnpm installcommand is used instead.
Note that you must ensure that the yarn.lock or pnpm-lock.yaml file is
not specified in the skip_files section of your app.yaml file.
By default, App Engine caches fetched dependencies to reduce build times. To install an uncached version of the dependency, use the command:
gcloud app deploy --no-cache
Support and limitations for the package.json file in the standard
environment:
You can use any Linux-compatible Node.js package with App Engine, including packages that require native (C) extensions.
If the
enginesfield is specified in yourpackage.json, you must define a compatible Node.js version.All dependencies that you define under the
devDependenciesfield are ignored and do not get installed for your app in App Engine.The
startscript should be lightweight and exclude build steps for the best performance because the script runs each time a new instance of your application is created.
Private dependencies with Artifact Registry
If your app uses a private npm module,
you can use an Artifact Registry Node.js package repository
to host private dependencies. When deploying your app, the build process
automatically generates Artifact Registry credentials for the
Cloud Build service account
so you won't need to generate additional credentials.
To include private dependencies, list the Artifact Registry repository and
configure settings for authenticating with the registry in your .npmrc
file in the app directory. For example:
@SCOPE:registry=https://REGION_ID-npm.pkg.dev/PROJECT_ID/REPOSITORY_NAME
//REGION_ID-npm.pkg.dev/PROJECT_ID/REPOSITORY_NAME:always-auth=true
Note that this approach also works for Yarn v1 package manager.
If you are using Yarn v2 or higher, list the Artifact Registry repository and
configure settings for authenticating with the registry in your .yarnrc.yml
file in the app directory. For example:
npmScopes:
SCOPE:
npmRegistryServer: https://REGION_ID-npm.pkg.dev/PROJECT_ID/REPOSITORY_NAME
npmAlwaysAuth: true
Afterwards, include your packages in the package.json file. For example:
{
"dependencies": {
" @SCOPE/my-package": "^1.0.0"
}
}
Installing a web framework
You'll need to use a web framework to enable your app to serve web requests. You can use any Node.js web framework including the following:
To use a particular web framework, such as Express.js, add the framework to your package.json file:
For example, the resulting package.json file might look as follows:
{
"dependencies": {
"lodash": "^4.0.1",
"express": "^4.16.2"
}
}
Installing the Cloud Client Libraries
The Cloud Client Libraries for Node.js is the idiomatic way for Node.js developers to integrate with Google Cloud services, such as Firestore in Datastore mode (Datastore) and Cloud Storage.
To install the Node.js client library for Cloud Storage:
Install the Cloud Client Libraries locally by using a package manager:
To use
npm, run:npm install @google-cloud/storage
To use
yarn, run:yarn add @google-cloud/storage
To use
pnpm, run:pnpm add @google-cloud/storage
Set up authentication. You can configure the Cloud Client Libraries for Node.js to handle authentication automatically.
Use the Node.js client library for Cloud Storage reference to implement support for the Cloud Storage service in your app.