Google Auth Library for Node.js
This is Google's officially supported client library for Node.js, providing comprehensive functionality for OAuth 2.0 authorization and authentication with Google APIs. It abstracts away the complexities of credential management, offering various authentication flows including Application Default Credentials (ADC), OAuth2 for user consent, JSON Web Tokens (JWT) for service-to-service communication, and direct access for Google Compute environments. The library is currently at version 10.6.2 and undergoes continuous development, with regular bug fixes and feature enhancements released typically on a monthly basis, as evidenced by recent changelog entries from March and February 2026. Key differentiators include its robust support for workload and workforce identity federation, impersonated credentials, and downscoped clients, making it suitable for a wide array of use cases from local development to complex multi-cloud deployments. It ships with TypeScript types, ensuring a typed developer experience.
Common errors
-
Error: Node.js v16.x.x is not supported.
cause The installed version of `google-auth-library` requires Node.js v18 or newer.fixUpgrade your Node.js runtime to version 18 or higher. For example, using nvm: `nvm install 18 && nvm use 18`. -
TypeError: require is not a function
cause Attempting to use CommonJS `require()` syntax within an ES Module (ESM) context, or a general module import mismatch.fixIf your project is ESM, ensure `"type": "module"` is set in `package.json` and use `import` statements. If CommonJS, verify no conflicting ESM configurations. -
Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
cause Application Default Credentials (ADC) could not be automatically located or configured in the current environment.fixAuthenticate using `gcloud auth application-default login`, set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to a service account key file path, or ensure your application runs within a Google Cloud environment with associated service accounts.
Warnings
- breaking The library explicitly requires Node.js v18 or newer. Running on older versions will result in runtime errors or unexpected behavior.
- gotcha While the library supports CommonJS, it is primarily designed for ES Modules. Using incorrect import syntax (e.g., `require` in an ES Module context) can lead to 'require is not defined' or `undefined` module exports.
- breaking The `gtoken` dependency, previously exposed in some internal contexts, was internalized within the `google-auth-library` since v10.6.0. Direct access or manipulation of `gtoken` objects is no longer officially supported and may break.
- gotcha Application Default Credentials (ADC) require proper environment configuration (e.g., `GOOGLE_APPLICATION_CREDENTIALS` environment variable, `gcloud` login, or running on GCP/GKE) to function automatically. Without it, authentication calls will fail.
- gotcha Updates to underlying HTTP proxy agents (e.g., `http-proxy-agent` updated to v7 in `teeny-request`, which is used by this library) may require adjustments if custom proxy configurations are used, potentially causing connection issues.
Install
-
npm install google-auth-library -
yarn add google-auth-library -
pnpm add google-auth-library
Imports
- GoogleAuth
const GoogleAuth = require('google-auth-library').GoogleAuth;import { GoogleAuth } from 'google-auth-library'; - OAuth2Client
import OAuth2Client from 'google-auth-library';
import { OAuth2Client } from 'google-auth-library'; - JWT
const JWT = require('google-auth-library').JWT;import { JWT } from 'google-auth-library';
Quickstart
import { GoogleAuth } from 'google-auth-library';
async function authenticateWithADC() {
const auth = new GoogleAuth({
scopes: 'https://www.googleapis.com/auth/cloud-platform',
});
// Get the credentials for the default project
const client = await auth.getClient();
const accessToken = await client.getAccessToken();
console.log('Successfully obtained access token using Application Default Credentials.');
console.log('Access Token:', accessToken.token);
// Example: Use the token to make an authenticated request
// (This is illustrative; actual API calls would use a client library or fetch with Authorization header)
if (accessToken.token) {
const url = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + accessToken.token;
try {
const response = await fetch(url);
const data = await response.json();
console.log('Token Info:', data);
} catch (fetchError) {
console.error('Failed to verify token:', fetchError);
}
} else {
console.warn("No access token found. Ensure ADC is configured correctly (e.g., GOOGLE_APPLICATION_CREDENTIALS set or running on GCP).");
}
}
authenticateWithADC().catch(console.error);