Google Cloud client library cannot find Application Default Credentials in Node.js
Could not load the default credentialsShort 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.
Fix it fast
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.json2Match what you see, then jump to its fix
- 1
Error on your laptop, and the credentials file in the first step is missingLocal machine: you never created user credentials for the librariesFix - 2
Error 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
Error on another cloud, a CI runner or an on-premises serverRunning outside Google Cloud with no credential file configuredFix
- 1
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.json2Fix
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 login3Verify
Run your script again.
node app.jsConfirmed by docs.cloud.google.com
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 console2Fix
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=ROLE3Verify
Restart the app on the resource and run the failing call again.
node app.jsConfirmed by docs.cloud.google.com
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.json3Verify
Run the failing call in the same shell.
node app.jsConfirmed by docs.cloud.google.com
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix