Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Google Cloud client library cannot find Application Default Credentials in Node.js

ErrorCould not load the default credentials

Short answer

Google client libraries look for Application Default Credentials in a fixed order: the GOOGLE_APPLICATION_CREDENTIALS variable, the file made by gcloud auth application-default login, then the service account attached to the machine. This error means none exist. Create the one that matches where your code runs.

  • Applies to Node.js 22, Google Cloud CLI, Application Default Credentials
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check which of the three sources exist on this machine. The one you expect to be there is the one that is missing.

    echo "$GOOGLE_APPLICATION_CREDENTIALS"
    ls ~/.config/gcloud/application_default_credentials.json
  2. 2Match what you see, then jump to its fix

    1. 1Error on your laptop, and the credentials file in the first step is missingLocal machine: you never created user credentials for the librariesFix
    2. 2Error on a Compute Engine VM or similar, and no GOOGLE_APPLICATION_CREDENTIALS is setRunning on Google Cloud without a service account attached to the resourceFix
    3. 3Error on another cloud, a CI runner or an on-premises serverRunning outside Google Cloud with no credential file configuredFix

What does the error look like?

  • Could not load the default credentials

Cause 1 of 3

Local machine: you never created user credentials for the libraries

1Check

gcloud auth login does not help here: the gcloud CLI keeps its own credentials, separate from the ones client libraries use. The libraries read the application default credentials file.

ls ~/.config/gcloud/application_default_credentials.json

2Fix

Sign in once for the libraries. This writes the file to the location shown above on Linux and macOS (%APPDATA%\gcloud on Windows).

gcloud auth application-default login

3Verify

Run your script again.

node app.js

Confirmed by docs.cloud.google.com

Did this fix it?

Cause 2 of 3

Running on Google Cloud without a service account attached to the resource

1Check

On Google Cloud the library gets credentials from the metadata server, which only works when a service account is attached to the VM or service. Google calls this the preferred method for production.

echo "[$GOOGLE_APPLICATION_CREDENTIALS]"  # empty, and no service account shown on the resource's detail page in the console

2Fix

Create a user-managed service account, grant it only the roles the code needs, and attach it to the resource that runs the code. The person attaching it needs the Service Account User role on that service account.

gcloud iam service-accounts create my-app-sa
gcloud projects add-iam-policy-binding PROJECT_ID \
  --member="serviceAccount:my-app-sa@PROJECT_ID.iam.gserviceaccount.com" \
  --role=ROLE

3Verify

Restart the app on the resource and run the failing call again.

node app.js

Confirmed by docs.cloud.google.com

Did this fix it?

Cause 3 of 3

Running outside Google Cloud with no credential file configured

1Check

Outside Google Cloud there is no metadata server. The first place the library looks is the GOOGLE_APPLICATION_CREDENTIALS variable, which must point to a credential configuration file.

echo "$GOOGLE_APPLICATION_CREDENTIALS"
ls -l "$GOOGLE_APPLICATION_CREDENTIALS"

2Fix

Point the variable at a Workload Identity Federation configuration file where you can. A service account key file also works but Google says keys are a security risk and not recommended.

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credential-config.json

3Verify

Run the failing call in the same shell.

node app.js

Confirmed by docs.cloud.google.com

Did this fix it?

How do I stop it coming back?

  • Use an attached service account on Google Cloud so no key file exists to leak.
  • On your laptop use gcloud auth application-default login, not a downloaded key.
  • Grant the service account the smallest role the code needs.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.