Publishing an npm package

Intermediate · 8 min

Semver, what npm pack ships, dist-tags and prereleases, 2FA, and publishing from CI with provenance and trusted publishing instead of long-lived tokens.

Before thispackage.json fields and exportsnpm scripts and lifecycle hooks

gleb bahmutov · 8 min

Publishing an npm package

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by gleb bahmutovReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Semantic versioning is a contract: patch (`1.2.3` to `1.2.4`) for fixes, minor (`1.3.0`) for backwards-compatible additions, major (`2.0.0`) for breaking changes. `^1.2.0` accepts any `1.x` at or above `1.2.0`; `~1.2.3` accepts only `1.2.x`. For `0.x` versions the caret is stricter: `^0.2.0` accepts only `0.2.x`.
  • Before publishing, run `npm pack --dry-run`. It lists exactly what will ship. Use the `files` field to allow-list `dist`, and expect npm to always add `package.json`, README and LICENSE.
  • `npm publish` puts the new version on the `latest` dist-tag by default. Publish prereleases with `--tag next` (or `beta`) so that a plain `npm install pkg` never picks them up.
  • A published version is permanent in practice: you can deprecate it, but you cannot reuse its number. Verify with a dry run, then publish.
  • Publishing from CI with trusted publishing (OIDC) replaces a stored npm token with a short-lived credential tied to a specific repository and workflow, and publishes provenance that links the tarball to its source and build.

Publishing is a promise to strangers

When you publish a package, other people’s builds start to depend on a name, a version number and the files you uploaded. You can add new versions, but you cannot replace an old one: registry versions are immutable in practice. So the habit that matters is checking before you publish. The mechanics are small.

Semantic versioning

A version is MAJOR.MINOR.PATCH:

  • Patch (1.2.3 to 1.2.4): bug fixes, no API change.
  • Minor (1.2.3 to 1.3.0): new features, backwards compatible.
  • Major (1.2.3 to 2.0.0): anything that can break existing users.

Consumers rely on this when they write ranges in their package.json. Checked with the semver library that ships inside npm:

Range Matches Does not match
^1.2.0 1.4.2 2.0.0
~1.2.3 1.2.9 1.3.0
^0.2.0 0.2.5 0.3.0
^1.0.0 1.4.0 1.5.0-beta.1 (prereleases are excluded)

The caret ^ allows changes that do not touch the leftmost non-zero number, so for 0.x it is as tight as a tilde. A package that says 0.x has told the world “anything can change”.

npm version updates package.json for you and, in a git repo, makes a commit and a tag:

npm version patch                          # 0.1.0 to 0.1.1
npm version minor                          # to 0.2.0
npm version prerelease --preid=beta        # 1.0.0-beta.1 to 1.0.0-beta.2

Check what will ship

The tarball is the product. List it before you upload anything:

npm pack --dry-run

For a package with "files": ["dist"] and extra src/ and test/ folders, the output lists only LICENSE, README.md, dist/index.js and package.json, and no source or tests. npm always adds the README, LICENSE and package.json. Without a files field, npm ships nearly everything that is not in .npmignore or .gitignore, which is how .env files and test fixtures end up on the public registry. The prepack script runs during npm pack, so if your package needs a build, that is the place for it. The field details are in package.json fields and exports.

Publish, with tags

npm login
npm publish --access public      # scoped packages default to private; public needs this

A scoped name such as @acme/tiny is private unless you pass --access public. The first publish claims the name; later publishes need a new version number each time.

Every publish also sets a dist-tag, a movable label pointing at a version. The default is latest, which is what npm install pkg uses. For anything you do not want everyone to receive, publish with another tag:

npm version 2.0.0-beta.1 --no-git-tag-version
npm publish --tag beta           # latest still points at the stable release
npm install your-pkg@beta        # testers opt in
npm dist-tag add your-pkg@1.9.0 latest   # move a tag later if you need to

If you forget --tag on a prerelease, npm refuses to publish a prerelease version to latest without it on current versions, which is a useful guard. If you publish a mistake, npm deprecate marks it with a warning; unpublishing is restricted and should not be your plan.

Accounts, 2FA and tokens

Publishing needs an npm account with two-factor authentication enabled, since publishing is the highest-impact action on an account. Locally you enter a one-time code with --otp. In CI you cannot type one, so you historically stored an automation token as a secret. Long-lived tokens are the weak point: a token in a CI secret, a laptop’s ~/.npmrc or a leaked log lets anyone publish as you. npm has been moving away from long-lived classic tokens, toward granular and short-lived ones, and toward trusted publishing.

Trusted publishing and provenance

Trusted publishing removes the stored token. You tell npm: “this package may be published by this GitHub repository, from this workflow file”. The workflow then asks GitHub for a short-lived OpenID Connect identity token on each run and npm accepts it, so there is no secret to leak. A minimal workflow needs the permission to request that identity token:

permissions:
  id-token: write
  contents: read
steps:
  - uses: actions/checkout@v4
  - uses: actions/setup-node@v4
    with: { node-version: 24, registry-url: 'https://registry.npmjs.org' }
  - run: npm ci
  - run: npm publish

Check the npm docs for the minimum npm and Node versions your CI needs, because they have moved. On npm 11.20 the CLI includes an npm trust command (npm trust github, npm trust list) to configure and inspect these relationships from the terminal; it needs 2FA on your account and the package must already exist, so the very first publish is done manually.

Provenance is a signed statement, published alongside the package, that records which repository, commit and workflow built the tarball. Consumers can verify that what is on the registry came from the source you see on GitHub. npm publish --provenance requests it explicitly from a supported CI, and it is attached automatically when you publish through trusted publishing from a public repository. It tells users where the code came from. It does not say the code is safe. The consumer side is covered in securing Node dependencies and the supply chain.

What the video covers

Gleb Bahmutov’s March 2026 video, per its description, shows the new trusted-publishing method of publishing npm packages from CI, using GitHub Actions, and says other supported CI providers work the same way. That is the modern half of this lesson. It assumes a package already exists, so semver, npm pack, files and dist-tags are the prose’s and were exercised locally with npm 11.20. Web Dev Simplified’s 2021 video under “go deeper” walks through a first manual publish end to end, with none of the trusted publishing material. No transcript was available. The claims about the exact YAML and version requirements should be checked against the linked npm docs.

Where this fits

This closes the modules and packages group. To use these ideas in your own code, the next group opens the standard library, starting with the file system. For TypeScript-specific publishing concerns such as shipping declaration files, see publishing a typed library.

Check yourself

  1. 01Which versions does `^1.2.0` accept, and which does `^0.2.0` accept?

    `^1.2.0` accepts anything from `1.2.0` up to but excluding `2.0.0`. `^0.2.0` accepts only `0.2.x`, because for `0.x` versions the minor number is treated as the breaking-change boundary. A test with the `semver` library confirmed `0.3.0` does not satisfy `^0.2.0`.

  2. 02You want to ship `2.0.0-beta.1` for testers without changing what `npm install your-pkg` gives everyone else. How?

    Set the version to `2.0.0-beta.1` and run `npm publish --tag beta`. The `latest` tag stays on the previous stable release, and testers install with `npm install your-pkg@beta`. Prerelease versions are also excluded from ranges like `^1.0.0` unless explicitly requested.

  3. 03What is the risk of a long-lived npm token stored as a CI secret, and what does trusted publishing change?

    A leaked token can publish malicious versions of your package from anywhere until someone revokes it. With trusted publishing, npm is configured to accept a publish only from a named repository and workflow, using a short-lived identity token that CI requests per run. There is no stored secret to steal.

  4. 04How do you check what files your package will contain before it goes public?

    Run `npm pack --dry-run`. It prints the tarball contents and size without creating or uploading anything. Adding a `files` allow-list to `package.json` is the usual fix when it shows source, tests or config you did not intend to ship.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark