Modern JavaScript applications often depend on packages from multiple registries. A project may use public packages from npm, private packages from an organization's registry, and internal libraries hosted through GitHub Packages or another package service.
To install these packages, developers need registry credentials. These credentials are usually access tokens that allow a package manager to authenticate with a registry.
The challenge is not simply storing a token. It is making sure the token is available when pnpm needs it without exposing it to source control, build logs, untrusted scripts, or other parts of the build environment.
A configuration that works on a developer's machine may also fail in a CI pipeline because environment variables, configuration files, and authentication settings are handled differently.
This article explains how pnpm manages registry credentials, how to configure authentication for local development and CI builds, and how to reduce the risk of exposing private registry tokens.
Understanding pnpm Registry Authentication
When pnpm downloads a private package, the package registry needs to verify that the requesting user or build process has permission to access it.
For example, an application might depend on an internal package:
{
"dependencies": {
"@company/authentication": "^2.1.0"
}
}
The package may be hosted on a private registry rather than the public npm registry.
Without authentication, pnpm may return an error such as:
ERR_PNPM_FETCH_401
Unauthorized
This usually means that the registry did not accept the supplied credentials or that no valid credentials were provided.
A registry access token is commonly used for authentication. The token is sent with registry requests so the registry can identify the client and check its permissions.
A token should be treated like a password. Anyone who obtains a valid token may be able to access private packages or perform other actions permitted by that token.
Understanding pnpm Authentication Configuration Files
pnpm supports authentication settings through npm-compatible configuration files and its global configuration.
Understanding where these settings are stored is important because not every configuration file is equally safe for storing credentials.
Project-level .npmrc
A project-level .npmrc file is stored in the repository, usually at the workspace root.
It is useful for sharing non-secret registry configuration with the development team.
For example:
@company:registry=https://registry.example.com/
This tells pnpm to use the specified registry for packages under the company scope.
However, the project-level file should not contain actual access tokens.
A committed configuration such as the following is unsafe:
//registry.example.com/:_authToken=actual-secret-token
Anyone with access to the repository may be able to retrieve the token.
Even this configuration requires care:
//registry.example.com/:_authToken=${NPM_TOKEN}
Recent pnpm authentication protections prevent environment variable expansion for credential settings in an untrusted project-level .npmrc. This restriction exists because a checked-out repository could otherwise influence where credentials are sent during installation.
The project-level file should therefore contain registry routing information, not secrets.
User-level auth.ini
pnpm maintains a user-level authentication file named auth.ini.
This file is stored in the pnpm configuration directory and is intended for authentication settings that should not be committed to the repository.
The configuration directory depends on the operating system.
Operating system | Typical pnpm configuration directory |
|---|
Windows | AppData/Local/pnpm/config |
macOS | Library/Preferences/pnpm |
Linux | .config/pnpm |
The actual location can also depend on environment variables and pnpm configuration.
You can log in to a registry using:
pnpm login
For a private registry, specify its URL:
pnpm login --registry=https://registry.example.com/
For a scoped registry, you can also specify the package scope:
pnpm login --registry=https://registry.example.com/ --scope=@company
pnpm stores the resulting authentication information in its user-level configuration.
This is generally a better option for local development than placing credentials inside the project repository.
Global configuration
pnpm also uses a global config.yaml file for settings that apply to the user's environment.
Some structured authentication settings are supported in this file.
The global configuration is useful when a developer needs to access several registries or package scopes from different projects.
However, the file should still be protected because it may contain sensitive credentials.
Configuring Private Registry Authentication
Consider a project that uses public npm packages and private packages from an organization.
The package configuration might look like this:
{
"dependencies": {
"express": "^5.0.0",
"@company/authentication": "^2.1.0",
"@company/logging": "^1.5.0"
}
}
The company packages are hosted on a private registry.
The project-level .npmrc can define the registry without including a token:
@company:registry=https://registry.example.com/
For local development, authenticate using pnpm login:
pnpm login --registry=https://registry.example.com/ --scope=@company
After authentication, install the project dependencies:
pnpm install
pnpm can now use the stored credentials when requesting packages from the private registry.
This separation keeps the registry address in the project configuration while keeping the authentication information in a user-level configuration file.
Using Environment Variables for CI Builds
CI pipelines need registry credentials because they usually run on temporary or isolated build machines.
A developer may already be authenticated locally, but a new CI runner does not automatically have access to the developer's authentication files.
The recommended approach is to store the token in the CI provider's secret management system and make it available to the build process through an environment variable.
For example, create a CI secret named:
NPM_TOKEN
The value should contain a valid registry access token with only the permissions required by the pipeline.
There are several ways to provide this token to pnpm.
Using pnpm_config__auth
pnpm supports a structured authentication configuration through the pnpm_config__auth environment variable.
This approach is useful in CI because it allows credentials to be associated with specific registry URLs.
For example, a Linux-based CI step can configure the public npm registry:
export pnpm_config__auth='{
"https://registry.npmjs.org": {
"@": {
"authToken": "${NPM_TOKEN}"
}
}
}'
pnpm install --frozen-lockfile
The example uses an environment variable reference rather than writing the actual token into the configuration.
The structure associates the token with the registry URL. The @ entry represents credentials for the registry's default package scope.
For an organization-specific registry, the configuration can include a package scope:
export pnpm_config__auth='{
"https://registry.example.com": {
"@company": {
"authToken": "${COMPANY_NPM_TOKEN}"
}
}
}'
pnpm install --frozen-lockfile
In this example, the token is associated with packages under the company scope.
The environment variable should be supplied by the CI secret manager. Do not replace the variable reference with a real token in a committed workflow file.
pnpm also recognizes the uppercase PNPM_CONFIG__AUTH environment variable, which can be useful when a CI provider normalizes environment variable names.
Using pnpm config set
Another option is to write the token into the user-level authentication configuration during the CI job.
For example:
pnpm config set //registry.example.com/:_authToken "$COMPANY_NPM_TOKEN"
pnpm install --frozen-lockfile
This writes the authentication setting to pnpm's global configuration location rather than the project's committed .npmrc file.
The command should run in a protected CI environment where the token is available only to authorized jobs.
Avoid printing the configuration file or enabling shell tracing around commands that handle secrets.
Using a user-level .npmrc file
A CI pipeline can also create a temporary user-level .npmrc file.
For example:
mkdir -p "$HOME"
cat > "$HOME/.npmrc" <<EOF
@company:registry=https://registry.example.com/
//registry.example.com/:_authToken=${COMPANY_NPM_TOKEN}
EOF
pnpm install --frozen-lockfile
This example creates the file in the runner's home directory rather than the project repository.
The token is expanded by the shell when the file is created. The file should be protected and removed when it is no longer needed.
This approach is useful when a CI environment already uses npm-compatible authentication configuration.
Example: Configuring pnpm Authentication in GitHub Actions
A GitHub Actions workflow can retrieve a registry token from repository or environment secrets.
The following example demonstrates a private package installation workflow:
name: Install Dependencies
on:
push:
branches:
- main
pull_request:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 22
- name: Set up pnpm
uses: pnpm/action-setup@v4
with:
version: 12
- name: Install dependencies
env:
COMPANY_NPM_TOKEN: ${{ secrets.COMPANY_NPM_TOKEN }}
run: |
pnpm config set //registry.example.com/:_authToken "$COMPANY_NPM_TOKEN"
pnpm install --frozen-lockfile
- name: Run tests
run: pnpm test
This example uses GitHub Actions secrets to provide the token during dependency installation.
The registry URL and token must match the actual private registry configuration.
There are a few important considerations when adapting this workflow:
The token should be stored as a CI secret, not as a plain workflow variable.
The token should have read-only package permissions if the job only installs dependencies.
The workflow should not print the token or authentication configuration.
Pull requests from untrusted forks should not receive private registry credentials.
The package manager version should be aligned with the version used by the project.
The example uses a specific major version for Node.js and pnpm as an illustration. Production workflows should pin and maintain the versions approved by the project.
Protecting Credentials From Untrusted Repository Configuration
One important security concern is that a repository can contain configuration files and scripts that influence installation behavior.
For example, a malicious change could modify a registry URL or introduce an unexpected package installation script.
If a CI pipeline exposes a token to an untrusted job, the job may be able to access that token through its environment or other available mechanisms.
This is why protecting the token itself is not enough. The build process must also be trusted.
Consider the following practices.
Do not expose secrets to untrusted pull requests
Pull requests from external contributors should not automatically receive private registry tokens.
A safe approach is to separate workflows that validate untrusted changes from workflows that need access to private packages.
If private dependencies are required for testing, use a controlled process that does not expose privileged credentials to untrusted code.
Restrict token permissions
Create tokens with the minimum permissions required by the build.
For example, a pipeline that only downloads private packages generally does not need permission to publish packages or modify registry settings.
Use separate credentials for installation and publishing.
This reduces the impact if a token is accidentally exposed.
Keep project configuration free of secrets
Commit registry addresses and package scope mappings when the team needs to share them.
Keep authentication credentials in user-level files or trusted CI configuration.
A repository should not be responsible for deciding which secret values are available to a build.
Avoid printing authentication configuration
Commands such as the following can expose sensitive information:
cat ~/.npmrc
cat ~/.config/pnpm/auth.ini
pnpm config list
These commands may display authentication settings depending on the environment and configuration.
Do not use them in CI logs without first verifying that sensitive values are protected.
Handling Multiple Registries and Package Scopes
Larger applications may use more than one registry.
For example:
Public packages come from the npm registry.
Company packages come from an internal registry.
A separate team publishes packages through GitHub Packages.
Each registry may require a different token.
A project-level .npmrc can define the registry destinations:
@company:registry=https://registry.example.com/
@frontend:registry=https://npm.pkg.github.com/
The authentication configuration should associate each token with the correct registry and scope.
For example, a user-level auth.ini can contain:
//registry.example.com/:@company:_authToken=${COMPANY_NPM_TOKEN}
//npm.pkg.github.com/:@frontend:_authToken=${FRONTEND_NPM_TOKEN}
This configuration keeps the credentials associated with their intended registries.
It is important not to use one token for every registry unless the registry provider explicitly supports that arrangement.
When working with multiple registries, verify that package scopes point to the correct hosts and that the credentials have access to the required packages.
Common Authentication Problems and How to Troubleshoot Them
Registry authentication problems can look similar even when the underlying causes are different.
401 Unauthorized
This error usually indicates that the registry did not accept the supplied credentials.
Possible causes include:
The token is expired or revoked.
The token has been copied incorrectly.
The environment variable is not available to the job.
The token is associated with a different registry.
The token does not have permission to read the requested package.
Check the secret configuration and registry URL first. Avoid printing the token while troubleshooting.
403 Forbidden
A 403 response usually means that the registry understood the request but refused access.
Check whether the token has the required package permissions and whether the user or organization has access to the package.
Some registries also require organization-level authorization or additional package access configuration.
Environment variable is not expanded
If pnpm reports that an authentication setting contains an unsupported environment variable reference, check where the setting is defined.
The project-level .npmrc is not a trusted location for expanding credential environment variables.
Move the authentication setting to a user-level auth file, use pnpm_config__auth, or provide the credential through an appropriate trusted CI environment variable.
Authentication works locally but fails in CI
Local development machines often have credentials saved from an earlier login.
CI runners usually start without those credentials.
Make sure the pipeline creates or provides authentication before running the installation command.
Also verify that the secret is available to the specific workflow event. For example, secrets may not be available to workflows triggered by pull requests from forks.
Private packages resolve from the wrong registry
Check the package scope mapping in .npmrc.
For example:
@company:registry=https://registry.example.com/
Make sure the package name uses the expected scope and that the registry URL is correct.
When several registries are configured, verify that the authentication token is associated with the same registry that serves the package.
Best Practices for Secure pnpm Builds
A secure registry authentication setup should be consistent across local development and CI environments.
Consider the following practices:
Use pnpm login for local registry authentication.
Keep actual credentials out of committed project files.
Use user-level auth.ini or another trusted authentication location.
Store CI tokens in the provider's secret management system.
Use pnpm_config__auth when structured registry authentication is useful.
Associate tokens with the correct registry and package scope.
Use read-only tokens for dependency installation.
Keep publishing credentials separate from installation credentials.
Do not expose secrets to untrusted pull request workflows.
Avoid printing authentication files or configuration in build logs.
Review project-level registry configuration before running CI installation.
Test authentication changes with a clean CI run.
These practices help reduce the risk of accidental token exposure while keeping package installation predictable.
Conclusion
Registry credentials are an important part of JavaScript dependency management, especially when applications use private packages.
pnpm provides several ways to manage authentication, including local login, user-level auth files, global configuration, and environment-based settings for CI pipelines.
The most important principle is to keep secrets separate from repository-controlled configuration. Project files can define where packages are downloaded from, but actual credentials should come from trusted user-level configuration or protected CI secrets.
For production builds, use tokens with limited permissions, protect untrusted workflows, and avoid exposing authentication details in logs.
A well-designed authentication process makes local development and automated builds more reliable while reducing the risk of exposing private registry credentials.
Join the conversation! Your thoughts help the community grow.