# npm Repository Guide kkRepo supports npm `hosted`, `proxy`, and `group` repositories. Hosted repositories accept private package publication, proxies cache an upstream registry, and groups provide one read endpoint for private and upstream packages. ## Create The Repositories | Purpose | Recipe | Recommended configuration | | --- | --- | --- | | Private packages | `npm-hosted` | Blob store, online state, write policy, strict validation | | Public registry cache | `npm-proxy` | Remote URL `https://registry.npmjs.org/` and cache TTLs | | Unified installs | `npm-group` | Hosted before proxy | The examples use `npm-hosted`, `npm-proxy`, and `npm-group` as repository names. ## Configure Installs Set the group in a user or project `.npmrc`: ```ini registry=https://nexus.example.com/repository/npm-group/ always-auth=true ``` For a private scope, keep the scope and token bound to the same repository root: ```ini @acme:registry=https://nexus.example.com/repository/npm-group/ //nexus.example.com/repository/npm-group/:_authToken=${NPM_TOKEN} ``` Use an `NpmToken` or another credential explicitly supported by the deployment. Keep user-level credentials outside the project repository. ## Publish A Package Authenticate and publish directly to hosted: ```bash npm login --auth-type=legacy --registry=https://nexus.example.com/repository/npm-hosted/ npm publish --registry=https://nexus.example.com/repository/npm-hosted/ ``` pnpm users can also log in directly. pnpm's web-login probe automatically falls back to the same legacy token login flow: ```bash pnpm login --registry=https://nexus.example.com/repository/npm-hosted/ ``` Use `publishConfig.registry` in `package.json` when a package must never be sent to the public registry. Run `npm pack --dry-run` before publication to inspect the tarball contents, and use `npm whoami --registry=...` to verify credentials. ## Repository Behavior - Hosted publication stores package metadata, tarballs, integrity values, and dist-tags together. - Proxy responses rewrite upstream tarball URLs to the kkRepo endpoint and cache metadata and content independently. - Group reads preserve member priority and keep metadata/tarball resolution on the selected source. - npm audit compatibility is exposed for supported client workflows; policy enforcement remains a separate kkRepo security-scanning concern. ## Hosted Write Policy With `ALLOW_ONCE` (the default), a hosted tarball cannot be overwritten, including when publishers race on different replicas. The database's unique asset path decides the winner; the losing publication receives the existing write-policy error and rolls back all its attachment and package-metadata changes. Refresh metadata to inspect the winner before retrying with a new version. Adding other versions still updates the package root normally. Multipart publication has the same transaction boundary. Unreferenced uploaded objects are cleaned up after a rollback. Transient database contention retries the complete publication in a fresh transaction, using the same staged attachments and revalidating metadata and write policy. Retries are bounded; unexpected failures retain their original cause. `ALLOW` retains tarball overwrite behavior. The npm publication endpoint does not provide an opt-in atomic create-if-absent contract for `ALLOW` repositories: a prior GET returning `404` is not a reservation. Use `ALLOW_ONCE` for immutable hosted publication. Proxy-cache refreshes and separately authorized administrative package-root cleanup retain their existing semantics. ## Operations And Troubleshooting Grant publication rights only on hosted. A `401` normally indicates invalid or missing credentials; a `403` means the authenticated identity lacks repository permission. After changing `.npmrc`, clear stale client state with `npm cache clean --force` only when normal retry and metadata refresh do not resolve the problem. ## Related Documentation - [npm client recipe](../client-recipes.md#npm) - [Compatibility matrix](../compatibility-matrix.md#repository-format-matrix) - [npm registry configuration](https://docs.npmjs.com/misc/registry/) - [`npm publish` reference](https://docs.npmjs.com/cli/publish/) ## Proxy Tarball URLs Proxy downloads follow the `dist.tarball` URL in the upstream package metadata. This includes Huawei CodeArts paths such as `@scope/package/-/@scope/package-1.0.0.tgz`; existing lockfiles using that scoped path are also accepted. The original metadata is persisted in shared blob storage, so another replica can resolve the same URL. A different download host must be listed in the proxy redirect-host allowlist; upstream credentials remain scoped to the trusted origin. A direct lockfile download also fetches missing or expired package metadata before selecting the upstream tarball URL. Scoped filename paths remain distinct from plain filenames, including in minimum-release-age checks. Older release-age indexes are rebuilt lazily from the stored raw metadata after upgrade; normal downloads do not require an operator backfill. Tarball suffix subdirectories are preserved. If metadata is unavailable, a direct download can still try the conventional registry path. Cached HTTP validators are reused only for the exact upstream URL they came from, including its query string.