skip to content

With npm 12, how do you publish `[email protected]` without moving the `latest` dist-tag, and what happens if you leave out `--tag`?

level: middleimportance: should knowfreq 14%

answer

  1. labels in front of versions
  2. publish moves latest by default
  3. a flag that picks another label
  4. prereleases refuse the default since npm 11
  5. add, rm and ls on tags

basics

~20 s

Run npm publish --tag beta: the version goes up under the beta dist-tag and latest stays on 2.x. Since npm 11, publishing a prerelease version without --tag (or a publishConfig tag) stops with an error instead of moving latest.

solid answer

~50 s

A **dist-tag** is a name that points at one published version, and `latest` is the one a bare `npm install a11y-datepicker` resolves. `npm publish` sets `latest` to the new version unless `--tag` is given, so the beta goes up with `npm publish --tag beta` (or `"tag": "beta"` in `publishConfig`), and testers opt in with `npm install a11y-datepicker@beta`. On npm 11 and 12, forgetting the flag for a prerelease version fails with "You must specify a tag using --tag when publishing a prerelease version." — npm 10 and earlier silently moved `latest` to the beta. A related npm 11 check refuses to tag `latest` implicitly when a higher stable version already exists, which is what a 2.x backport after 3.0.0 hits. Afterwards, `npm dist-tag ls`, `npm dist-tag add <pkg>@<version> <tag>` and `npm dist-tag rm <pkg> <tag>` manage the labels.

code

bash · 14 lines
bash
# publish the beta under its own tag; latest stays on 2.x
npm publish --tag beta

# check where each tag points
npm dist-tag ls a11y-datepicker
# beta: 3.0.0-beta.1
# latest: 2.4.2

# testers opt in explicitly
npm install a11y-datepicker@beta

# release day: 3.0.0 is stable and highest, so publish moves latest
npm publish
npm dist-tag rm a11y-datepicker beta

go deeper

for a junior

Recall that latest is what a bare npm install gets, and that npm publish --tag beta keeps a prerelease off it.

for a middle

Explain the npm 11 checks: a prerelease needs an explicit tag, and latest is never applied implicitly to a version lower than the highest stable one.

for a senior

Run a release line safely: betas under their own tag, backports under a maintenance tag, and npm dist-tag add to repair a wrongly moved latest.

for a principal

Set a tagging convention for every package the team publishes, encode it in publishConfig or the release job, and decide who may move latest.

## Dist-tags: names in front of version numbers Every published npm version has a fixed number, such as `2.4.2` or `3.0.0-beta.1`, that can never be reused. On top of those numbers the registry keeps **dist-tags**: human-readable names that each point at exactly one version and can be moved at any time. - `latest` is the only tag with special meaning to npm. `npm install a11y-datepicker` with no version or tag installs whatever `latest` points at. - Every other name — `beta`, `next`, `canary`, `maintenance` — is a convention. `npm install a11y-datepicker@beta` installs whatever `beta` points at, and fails if no such tag exists. - Tags share a namespace with version specifiers, so a tag that is a valid semver range is rejected: `v1.4` cannot be a tag because it means `>=1.4.0 <1.5.0`. Names that do not start with a digit or `v` avoid the problem. ## What `npm publish` does with `latest` The `tag` config defaults to `latest`, and `npm publish` adds the new version under that tag. Publishing therefore **moves `latest`** unless you choose another tag: ```bash npm publish --tag beta ``` The same can be pinned in the manifest with `"publishConfig": { "tag": "beta" }`; a `--tag` on the command line wins over `publishConfig`. Pinning the tag in `publishConfig` has a cost: it applies to **every** publish from that manifest, so if it is still there on release day the stable `3.0.0` also goes up under `beta` and `latest` never moves. A tag passed on the command line in the release job is easier to see and to change. Testers can also install an exact prerelease, `npm install [email protected]`, which does not depend on any tag at all. Users already depending on a range such as `^2.4.0` are unaffected either way: that range does not match a `3.0.0` prerelease, and `npm install --tag beta` does not override ranges in `package.json`. ## The prerelease rule (npm 11 and later) Before npm 11, `npm publish` on `3.0.0-beta.1` with no tag moved `latest` to the beta, and every new install of the package got unreleased code. npm 11.0.0 made two changes, both still in npm 12: 1. **A prerelease version needs an explicit tag.** If the version has a prerelease part and neither `--tag` nor `publishConfig.tag` is set, the publish stops with *"You must specify a tag using --tag when publishing a prerelease version."* 2. **`latest` is never applied implicitly to a lower version.** Before tagging, npm fetches the published versions; if the highest stable version (prereleases and deprecated versions excluded) is greater than or equal to the one being published and no tag was given, it stops with *"Cannot implicitly apply the "latest" tag because previously published version 3.0.0 is higher than the new version 2.4.3. You must specify a tag using --tag."* Both checks are skipped under `--force`, which defeats their purpose; the fix is to name the tag. ## Managing tags after publishing | Command | Effect | |---|---| | `npm dist-tag ls a11y-datepicker` | Lists every tag and its version | | `npm dist-tag add [email protected] latest` | Points `latest` at an already-published version | | `npm dist-tag rm a11y-datepicker beta` | Removes a tag that is no longer in use | | `npm install a11y-datepicker@next` | Installs the version a tag points at | `npm dist-tag add` without a tag name uses the `tag` config, which is `latest` by default — so a forgotten argument moves `latest`. ## Release day and backports A typical beta line for a 3.0.0 major runs like this: 1. Publish each beta with `npm publish --tag beta`; `latest` stays on the 2.x line. 2. When `3.0.0` is ready, publish it without a tag: it is a stable version above `2.4.2`, so `latest` moves to it. 3. Remove the stale label with `npm dist-tag rm a11y-datepicker beta`, or leave it for the next prerelease cycle. 4. A later fix on the 2.x line, such as `2.4.3`, needs its own tag — `npm publish --tag maintenance` — because `latest` now points at the higher `3.0.0`. In an interview, the point to land is that a tag is a movable pointer, a version is permanent, and npm 11 and later refuse to move `latest` by accident in the two cases where that used to happen.

  • After 3.0.0 is out, how do you publish a 2.4.3 fix on npm 12, and what happens if you run plain `npm publish`?
    Plain `npm publish` fails: npm sees that the published `3.0.0` is higher than `2.4.3` and refuses to apply `latest` implicitly, asking for `--tag`. Publish it as `npm publish --tag maintenance` (any non-semver name), so `latest` stays on `3.0.0` and 2.x users with a `^2.4.0` range still pick up the fix.
  • Why does npm reject a dist-tag named `v1.4`?
    Tags and version specifiers share one slot in `name@<spec>`, so npm refuses any tag that is a valid semver range. `v1.4` parses as the range `>=1.4.0 <1.5.0` and fails with "Tag name must not be a valid SemVer range". Names that do not start with a digit or `v` avoid the clash.

Dist-tags are signposts in front of a row of numbered houses. The houses never move and no number is ever reused; publishing builds a new house and, unless told otherwise, moves the 'latest' signpost to it, while a 'beta' signpost can point somewhere else entirely.

  • Publishing a beta version leaves latest alone automatically
  • npm derives the dist-tag from the prerelease name in the version
  • npm install --tag beta upgrades dependencies pinned by package.json ranges
  • Moving latest back needs a new publish of the older version
  • A dist-tag can be named v2 to mark the 2.x line

Assumes npm 12 (checked against 12.2.0); the prerelease --tag requirement and the refusal to apply latest implicitly to a lower version arrived in npm 11.0.0, and npm 10 and earlier moved latest silently.