First package? Follow the publish and install quickstart →
Overview
This guide tells you how to configure public npm caching on a registry. With public npm caching, one registry URL serves your private packages and approved public packages. The public packages come from the public npm registry. npm clients then need only one registry and one credential.
The registry sends a request to the public registry only for an external name. An external name is not hosted, and no one claimed its scope. A policy controls which public names the registry serves. The policy has a mode, allow and block patterns, and a minimum release age. A private package that does not exist never resolves from the public registry.
This guide is for administrators who configure npm registries and for security teams who examine dependency risk. Public npm caching applies to npm only. PyPI and Files registries do not use public npm caching.
Before you start
- Plan. Public npm caching is part of the Growth, Scale and Platform plans. Starter does not include it. Refer to pricing.
- Role. You must have the Admin role. Only the Admin role has the
npm:registry:managepermission that changes the policy. - Registry. You can use the shared registry at
registry.privatenpm.comor a private npm registry. On the shared registry, each organization has its own policy. - Credential. You must have a download key or a publisher key for the test installs.
- Dependency list. Prepare a list of the public packages that your projects use. You need the list for an allowlist.
How it works
For each package request, the registry does the checks below in this order. The first check that applies gives the result.
- The registry authenticates the credential. An invalid credential gets 401.
- The registry classifies the name. The class is Hosted, Claimed scope or external.
- For a hosted name or a claimed scope, the registry uses the private path only. The result is the package, 403 or 404.
- For an external name, the registry examines the plan, the credential and the policy.
- If all three permit the name, the registry serves the package from the cache or from the public registry.
Name classification
A name is Hosted when a package with that name exists in the registry. Deleted packages and unpublished packages count. Thus a hosted name stays reserved after you remove its versions. The check includes the packages of all organizations on the registry.
A scoped name is a Claimed scope when any organization claimed that scope on the registry. On the shared registry, all claimed scopes count. For example, if Acme holds @acme, no organization can receive @acme/sdk from the public registry.
All other names are external. The registry classifies the name again for each request. If you publish a name that the registry served from the public registry before, the name becomes hosted at once. The registry then ignores the cached copy for that registry.
The policy
The policy decides which external names the registry serves. The Mode field has three values:
| Mode | External names that the registry serves |
|---|---|
| Off | None. Off is the default. |
| Public | All external names that no block pattern matches. |
| Allowlist | Only external names that an allow pattern matches and no block pattern matches. |
A block pattern applies in all modes. A block pattern also has priority over an allow pattern.
Each pattern is a glob in the format of the Go path.Match function. A star (*) matches any characters except a slash. Thus * alone matches no scoped name. The table shows examples.
| Pattern | Matches | Does not match |
|---|---|---|
react | react | react-dom |
react* | react, react-dom | @types/react |
@types/* | @types/node, @types/react | types |
@*/* | All scoped names | lodash |
* | All unscoped names | @babel/core |
Write one pattern on each line. Each list accepts 64 patterns or fewer, and each pattern has 200 characters or fewer. The registry removes blank lines and duplicate patterns. Patterns are case-sensitive, and npm names are lowercase. Thus write patterns in lowercase.
Minimum release age
The Minimum release age (hours) field accepts a value from 0 to 720. The registry hides each upstream version that is newer than this age. The registry uses the publish time in the package metadata from the public registry. A version without a publish time stays visible.
If a dist-tag such as latest points to a hidden version, the registry moves the tag to the newest visible version. If no version is visible, the registry returns 404 for the package. A tarball request for a hidden version also gets 404. A delay protects you from a malicious version that the public registry removes in its first days. A delay of 72 hours is a usual value.
Credential access
A credential must have its own permission for external names. A download key has the checkbox Allow this key to install public packages through the cache. The checkbox is off by default. A publisher key has the permission Read public packages. This permission is on by default for publisher keys that can read packages.
The package and scope restrictions of a download key do not apply to external names. If the checkbox is on, the key can install every external name that the policy permits. The key must also be valid for the registry.
The cache
The registry gets packages from https://registry.npmjs.org and sends no credential to the public registry. The registry keeps package metadata fresh for 5 minutes. After 5 minutes, the registry revalidates the metadata with an ETag. The registry also keeps a "not found" answer for 1 minute.
Before the registry stores a tarball, the registry compares the tarball with the SHA-512 integrity value in the metadata. If the values are different, the registry returns 502 and does not store the tarball. The maximum size of a tarball is 256 MiB. The registry changes each tarball URL in the metadata to the address of the registry. Thus your lockfiles contain your registry URL, not the public URL.
The platform owns the cache and shares stored tarballs between organizations. By default, the platform removes cache entries that no request used for 90 days.
Storage and transfer
The cached bytes do not count against the storage allowance of your plan. Each tarball that the registry serves from the cache counts as a download, and its bytes count as transfer. Thus cached downloads use the monthly transfer allowance and can cause overage. The registry records metadata requests, but metadata requests do not count as transfer.
The registry limits each organization to 600 cache misses per minute. A request above the limit gets 429. Cache hits have no limit.
Fail-closed behaviour
The registry never guesses. If the registry cannot complete a check, the registry refuses the request. The table gives the results.
| Condition | Result |
|---|---|
| The policy, the plan or the credential does not permit the name | 404 |
| The name is not a valid npm name, or the classification fails | 502 |
| The public registry has no such package or version | 404 |
| The public registry is not available, and a copy of the metadata is less than 24 hours old | The registry serves the copy |
| The public registry is not available, and no recent copy exists | 502 |
| The tarball does not agree with its integrity value | 502 |
After 5 failures in sequence, the registry stops requests to the public registry for 30 seconds. During that time, the registry serves recent copies only.
npm audit requests
The registry keeps no security advisories of its own. The Forward npm audit checkbox controls the answer to npm audit. When the checkbox is on, the registry sends a part of each audit request to the public registry. The registry then returns the advisories from the public registry for the names that it sent.
The registry examines each name in the audit request before the registry sends the request. The registry sends a name only when all of these conditions are true:
- The plan includes public npm caching, and the mode is not Off.
- The credential can install public packages. A download key must have the public packages checkbox.
- The name is external, and the policy permits the name.
The registry never sends a hosted name, a name in a claimed scope or a blocked name to the public registry. If the registry cannot classify a name, the registry does not send that name. For each of these names, the answer contains an empty list of advisories. The registry sends the names and their versions, but no credential.
When the checkbox is off, the registry sends nothing to the public registry. The answer then contains no advisories. npm 6 uses a different audit request. The registry does not forward npm 6 requests, and the answer contains no advisories.
The registry waits 10 seconds or less for the public registry. If the public registry does not answer in that time, or returns an error, the registry returns HTTP 503. The npm audit command then reports that the audit failed. The npm install command shows a warning and continues.
CAUTION: Do not use an empty audit result as proof that a project is safe. Hosted packages never get advisories, and names that the policy does not forward get no advisories.
Configure the policy
Make sure that your plan includes public npm caching. Without the plan, the page shows "Public package caching is not on this plan. Growth and above include it."
- Open Registry → Registries.
- If you configure the shared registry, click Public packages below Shared registry. The Public packages page opens.
- If you configure a private registry, click Manage registry. The Public packages card is on the registry page.
- Select Allowlist in Mode.
- Type your allow patterns in Allow patterns, one on each line.
- Type the names that you never want in Block patterns.
- Type
72in Minimum release age (hours). - If you want advisories for public packages from
npm audit, select Forward npm audit. - Click Save public packages. The message "Public packages saved." appears.
An example policy for a React project:
Mode: Allowlist
Allow patterns:
react
react-dom
scheduler
@types/*
@babel/*
Block patterns:
event-stream
Minimum release age (hours): 72CAUTION: Include the transitive dependencies in an allowlist. If one dependency is not on the list, npm install stops with E404 for that name.
To make the list of names in a lockfile, run this command in a project that has a package-lock.json file:
node -e 'const l=require("./package-lock.json");const n=new Set();for(const p of Object.keys(l.packages||{})){const i=p.lastIndexOf("node_modules/");if(i>=0)n.add(p.slice(i+13))}console.log([...n].sort().join("\n"))'The command writes one package name on each line. Remove your hosted names, then paste the result in Allow patterns. The management API offers the same policy at GET /api/v1/registries/{id}/upstream-policy and PUT /api/v1/registries/{id}/upstream-policy. Refer to the API reference.
Use the preview
- Find the Preview area below the policy.
- Type a name in Package name, for example
react. - Click Check. The result is Hosted, Claimed scope, Blocked or Will proxy.
The preview uses the saved policy. Save the policy before you use the preview. A name that the policy does not permit shows Blocked, also when no block pattern matches. The preview does not examine credentials.
Give credentials access
To let a download key install external names:
- Open Registry → Registry keys.
- Open the download key.
- In the Public packages card, select Allow this key to install public packages through the cache.
- Click Save. The message "Public package access saved." appears.
For a publisher key, make sure that the key has the Read public packages permission.
Configure npm
To send all installs through the registry, set the default registry and the credential for the same host. For a private registry on npm.acme.com:
registry=https://npm.acme.com/
//npm.acme.com/:_authToken=${ACME_NPM_TOKEN}For the shared registry, use registry.privatenpm.com in both lines. The registry uses the organization of the credential to select the policy. For CI and pnpm, refer to the .npmrc guide.
Verify the result
Use a clean directory, the .npmrc file above and a credential with access to external names. Then do the tests below.
- Run
npm view react version. The result is a version number. - If
event-streamis in Block patterns, runnpm view event-stream version. npm shows E404. - Run
npm view @acme/no-such-package versionwith the scope of your organization. npm shows E404, because your organization claimed the scope. - Run
npm view react dist-tags. Compare thelatestversion with the publish times fromnpm view react time --json --registry https://registry.npmjs.org/. Thelatestversion is older than the minimum release age. - Run
npm installin a project. Make sure that eachresolvedURL inpackage-lock.jsonstarts with your registry URL. - If Forward npm audit is on, run
npm auditin the same project. npm shows the advisories for the public packages.
Test 3 is the most important test. Test 3 proves that a name in your scope cannot come from the public registry. The This month panel then shows the cache hits, the misses and the bytes served.
Troubleshoot
| Symptom | Cause | Correction |
|---|---|---|
| E404 for a public package | The mode is Off, or the name is not on the allowlist, or a block pattern matches. | Use the preview. Add an allow pattern or remove the block pattern. |
| E404 for all public packages, with a correct policy | The credential has no access to external names. | Select the checkbox on the download key, or give the publisher key Read public packages. |
| E404 after a plan change | The plan does not include public npm caching. The registry keeps the policy but does not use it. | Change to Growth or a higher plan. |
| E404 for a new upstream release | The version is newer than the minimum release age. | Wait, or decrease the age. |
A scoped name does not match * | A star does not match a slash. | Add @scope/* or @*/*. |
| 502 from the registry | The public registry is not available, or a tarball failed its integrity check. | Try again later. If the error continues for one tarball, report the package name to support. |
| 429 from the registry | More than 600 cache misses in one minute. | Try again after one minute. Reduce parallel installs from empty caches. |
npm audit finds no vulnerabilities in a public package that has advisories | Forward npm audit is off, or the credential or the policy does not permit the name. | Select Forward npm audit, and examine the name with the preview. Then run npm audit again. |
npm audit reports that the audit endpoint returned an error (503) | The public registry did not answer in 10 seconds, or returned an error. | Run npm audit again later. The registry never reports a clean result for a failed request. |
| E401 | The credential line does not match the registry host. | Refer to the E401 checklist. |
Security and maintenance
Public npm caching protects against dependency confusion. In a dependency confusion attack, a person publishes a public package with the name of your private package. A registry that falls back to the public registry can then install the attack package. This registry never falls back for a hosted name or a claimed scope.
WARNING: Do not let a project or CI job run without the registry configuration. A project without the .npmrc file sends requests to registry.npmjs.org, and the protection of this registry does not apply.
- Least privilege. Use Allowlist mode for production builds. Use Public mode only where people need many new packages each week.
- Download keys. Select the public packages checkbox only on keys that need external names. Keys for recipients usually need only your packages.
- Release age. Keep a minimum release age on registries that build production releases.
- Audit. The audit log records each policy change. On Growth and higher plans, the webhook event
upstream_policy.updatedis available. Refer to the webhooks guide. - Concurrent edits. If a different administrator saved the policy after you opened the page, the registry refuses your change. Reload the page and make the change again.
- Review. Examine the allowlist when you add dependencies, and remove names that no project uses.