In npm's `.npmrc`, how do you send the `@harbor` scope to a private registry and give npm its token without committing the token?
answer
- routing and credentials are separate lines
- a scope maps to one registry
- auth keyed by host, not scope
- variables expanded when the file is read
- committed project file, private user file
basics
~10 sPut @harbor:registry=https://registry.harbor.example/ and //registry.harbor.example/:_authToken=${HARBOR_NPM_TOKEN} in the project .npmrc. npm expands the environment variable when it reads the file, so only the placeholder is committed.
solid answer
~40 sTwo lines do two jobs. `@harbor:registry=https://registry.harbor.example/` routes every install and publish of an `@harbor/*` package to that registry; a scope points at exactly one registry. The credential is a separate line keyed by **registry host**, not by scope — `//registry.harbor.example/:_authToken=${HARBOR_NPM_TOKEN}` — because npm requires `_authToken`, `_auth`, `username`, `_password`, `certfile` and `keyfile` to be scoped to a registry URI so it never sends a credential to the wrong host. npm substitutes `${HARBOR_NPM_TOKEN}` from the environment when it reads the file, so the project `.npmrc` can be committed with the placeholder while CI injects the secret. A developer's real token belongs in the per-user `~/.npmrc`, which `npm login --scope=@harbor --registry=https://registry.harbor.example/` writes. If the variable is unset, npm leaves the text unreplaced and the registry rejects it.
code
ini · 3 lines; project .npmrc, committed with the repository
@harbor:registry=https://registry.harbor.example/
//registry.harbor.example/:_authToken=${HARBOR_NPM_TOKEN}go deeper
Recall the two lines: a scope line that names the registry and a host-keyed _authToken line whose value comes from an environment variable.
Explain why auth is keyed by registry host, how the four .npmrc files rank, and why the project file holds only the placeholder.
Debug the usual failures: an unset variable sent literally, a host mismatch ending in ENEEDAUTH, and a scope mapping that lives on one laptop only.
Design token handling across teams: read-only install tokens, a narrow publish token, publishConfig pinning internal packages, and revocation when a token leaks.
## Routing and credentials are two different lines A **scope** is the `@name/` prefix on a package name, such as `@harbor/ui`. npm lets you associate a scope with a registry, so a project can install public packages from the default registry and `@harbor/*` packages from a private one in the same `npm install`. - **Routing**: `@harbor:registry=https://registry.harbor.example/` sends every install of an `@harbor/*` package to that registry, and every `npm publish` of a package whose name carries the scope goes there too. Scopes are many-to-one with registries: one registry can host several scopes, but a scope points at only one registry. - **Credentials**: `//registry.harbor.example/:_authToken=...` attaches a token to requests for that **host**. The leading `//host/:` is a URI fragment; a path can be included (`//host/some/path:`) when one host serves several registries. The two lines are independent. The scope line says where packages come from; the auth line says what to present when talking to that host. ## Why the token is keyed by host npm's `.npmrc` documentation says that `_auth`, `_authToken`, `username`, `_password`, `certfile` and `keyfile` must all be scoped to a specific registry, so that npm never sends credentials to the wrong host. In practice: - a token written for `registry.harbor.example` is never sent to the public registry, even though both are used in the same install; - there is no `@harbor:_authToken` form — scoping is by registry URI, not by package scope; - a bare `_authToken=` line with no host is an invalid entry; `npm config fix` repairs it by attaching it to the configured default registry. ## The four `.npmrc` files and what belongs in each npm reads configuration from the command line, from `npm_config_*` environment variables and from four files, in priority order: | File | Location | Typical contents | |---|---|---| | Per-project | `.npmrc` beside `package.json` | Scope routing, the `${VAR}` auth placeholder — committed | | Per-user | `~/.npmrc` | A developer's own tokens, written by `npm login` | | Global | `$PREFIX/etc/npmrc` | Settings for everyone using that npm installation | | Built-in | inside npm's own folder | Distribution defaults; not edited by hand | A setting in a higher-priority file wins over the same key lower down; a flag on the command line beats them all. The project file only applies when npm runs in that project, and it never travels with a published package — `.npmrc` is on npm's never-included list. ## Keeping the token out of Git 1. Commit a project `.npmrc` containing the scope line and `//registry.harbor.example/:_authToken=${HARBOR_NPM_TOKEN}`. npm replaces `${HARBOR_NPM_TOKEN}` with the environment variable's value when it reads the file. 2. In CI, store the token as a secret and expose it to the job as `HARBOR_NPM_TOKEN`. Prefer a read-only token for jobs that only install, and a separate, narrower token for the job that publishes. 3. On laptops, run `npm login --scope=@harbor --registry=https://registry.harbor.example/`. It stores the credential and the scope mapping in the per-user `~/.npmrc`, outside the repository. 4. To make every `@harbor` package publish only to the private registry, also set `"publishConfig": { "registry": "https://registry.harbor.example/" }` in each package; `publishConfig` values apply at publish time. If a literal token was ever committed, deleting the line is not enough: the token is in the Git history and must be revoked and replaced. ## Failures you will meet - **Unset variable.** By default npm leaves an undefined `${VAR}` unreplaced, so it sends the literal text `${HARBOR_NPM_TOKEN}` as the token and the registry rejects the request. Writing `${HARBOR_NPM_TOKEN?}` makes an unset variable evaluate to an empty string instead. - **Token keyed to the wrong host.** A hostname or path mismatch between the scope's registry URL and the auth line means npm finds no credential for that host; a publish then stops with `ENEEDAUTH`, "This command requires you to be logged in". - **Mapping only on one laptop.** If the `@harbor:registry` line lives in one developer's `~/.npmrc` rather than the committed project file, everyone else resolves `@harbor/*` against the default registry and gets a not-found error — or, worse, whatever someone else published under that name there. In an interview, the answer that lands is the pair of lines, the reason auth is keyed by host, and where each kind of setting lives.
- What does `npm login --scope=@harbor --registry=https://registry.harbor.example/` write, and to which file?It authenticates against that registry, stores the resulting credential keyed by the registry host, and records `@harbor:registry=https://registry.harbor.example/`, all in the per-user `~/.npmrc`. Nothing lands in the project, so nothing secret is committed; each developer runs it once per machine.
- A CI install of `@harbor/ui` fails authentication although the job has the right secret. Where do you look first in the npm setup?Check that the job really exports the variable named in the `.npmrc` placeholder — an unset `${HARBOR_NPM_TOKEN}` is sent literally — and that the host in the auth line matches the scope's registry URL, including any path. `npm config get @harbor:registry` shows where the scope resolves.
- The token goes on the scope line, as @harbor:_authToken
- A committed project .npmrc with a real token is fine if the repo is private
- An unset ${VAR} in .npmrc becomes an empty string by default
- npm sends the private registry token to every registry it contacts
- The project .npmrc is published inside the package tarball
- A scope can be split across several registries