Load secrets from Google Cloud Secret Manager into environment variables, similar to how dotenv
works.
This package also automatically sets the Google Cloud project ID as GOOGLE_PROJECT_ID
in your environment variables.
npm install @stagetimerio/google-secrets
// Programmatic usage
import { loadSecrets } from '@stagetimerio/google-secrets';
async function start() {
// Load secrets into process.env
await loadSecrets();
// Load specific secrets with a prefix
await loadSecrets({
secretKeys: ['GITHUB_TOKEN', 'DATABASE_URL'],
prefix: 'VITE_'
});
// Now you can use process.env.SECRET_NAME
console.log(process.env.MY_SECRET);
console.log(process.env.VITE_GITHUB_TOKEN);
// You can also access the Google project ID
console.log(process.env.GOOGLE_PROJECT_ID);
}
# Load all secrets from the default project
# This will automatically fetch all available secrets in the project
node -r @stagetimerio/google-secrets/load app.js
# Specify secrets to load via environment variable
GOOGLE_SECRETS_KEYS=API_KEY,DATABASE_PASSWORD node -r @stagetimerio/google-secrets/load app.js
# Use a JSON file to specify secrets
GOOGLE_SECRETS_KEYS_FILE=./secrets.json node -r @stagetimerio/google-secrets/load app.js
# Add a prefix to all loaded environment variables
GOOGLE_SECRETS_PREFIX=VITE_ GOOGLE_SECRETS_KEYS=GITHUB_TOKEN,API_KEY node -r @stagetimerio/google-secrets/load app.js
# Results in: VITE_GITHUB_TOKEN and VITE_API_KEY
# Override existing environment variables (default behavior preserves existing ones)
GOOGLE_SECRETS_OVERRIDE_EXISTING=true node -r @stagetimerio/google-secrets/load app.js
# Enable debug output
GOOGLE_SECRETS_DEBUG=true node -r @stagetimerio/google-secrets/load app.js
# Load .env first, then Google secrets
node -r dotenv/config -r @stagetimerio/google-secrets/load app.js
By default, the package will look for a google-secrets.config.json
file in the current directory and parent directories (similar to how ESLint finds .eslintrc
). This allows you to specify which secrets to load without setting environment variables.
Example google-secrets.config.json
:
[
"API_KEY",
"DATABASE_PASSWORD",
"JWT_SECRET"
]
Or with a secrets
property:
{
"secrets": [
"API_KEY",
"DATABASE_PASSWORD",
"JWT_SECRET"
],
"prefix": "VITE_",
"overrideExisting": true
}
The package uses the following priority when determining which secrets to load:
GOOGLE_SECRETS_KEYS
environment variable (comma-separated list)GOOGLE_SECRETS_KEYS_FILE
environment variable (path to a JSON file)- Auto-discovered
google-secrets.config.json
file - If none of the above are found, all available secrets from the project are loaded
The package uses a child process to load secrets when used with the -r
flag. This approach is necessary because Node.js's -r
flag mechanism expects synchronous module loading, but accessing the Secret Manager API requires asynchronous operations.
By spawning a separate child process, we can perform asynchronous API calls and then wait for them to complete before continuing with the main application execution.
The package automatically sets the Google Cloud project ID as an environment variable GOOGLE_PROJECT_ID
. This is determined from the authentication client and is always set, regardless of whether any secrets are loaded.
Config file: secrets
Env Var: GOOGLE_SECRETS_KEYS
Possible values: Array of secret names or comma-separated string
Default: undefined
(loads all secrets)
Config file: N/A
Env Var: GOOGLE_SECRETS_KEYS_FILE
Possible values: Path to JSON file
Default: undefined
Config file: prefix
Env Var: GOOGLE_SECRETS_PREFIX
Possible values: Any string
Default: undefined
Config file: N/A
Env Var: GOOGLE_SECRETS_DEBUG
Possible values: true
, false
Default: false
Config file: N/A
Env Var: GOOGLE_SECRETS_TIMEOUT
Possible values: Number (milliseconds)
Default: 5000
Config file: overrideExisting
Env Var: GOOGLE_SECRETS_OVERRIDE_EXISTING
Possible values: true
, false
Default: false
Description: When set to true
, will override existing environment variables with loaded secrets. When false
(default), existing environment variables are preserved.
Config file: N/A
Env Var: GOOGLE_SECRETS_AUTO_DISCOVER
Possible values: true
, false
Default: true
Programmatic options > Environment variables > Config file > Defaults
This package uses Google Cloud authentication. The simplest way to authenticate is by setting the GOOGLE_APPLICATION_CREDENTIALS
environment variable to point to your service account key file.
In cloud environments (like Google Cloud Run or GCP Compute Engine), authentication typically happens automatically.
Your scripts in package.json
might look like:
"scripts": {
"dev": "cross-env TZ='UTC' PORT=3000 SERVER_ID=local-3000 nodemon -r dotenv/config -r @stagetimerio/google-secrets/load --ignore 'scripts/*' --stack-trace-limit=20 --experimental-specifier-resolution=node _server.web.js"
}
ISC
- rimraf: v6 only supports Node 22