Sub topic 13
Versioning and publishing to npm
This chapter covers the last step: choosing a version number, releasing the package to npm, and handling mistakes afterward. It assumes you have a package that builds with tsc, a working exports map, and a clean result from npm pack --dry-run, publint and Are the Types Wrong. You should also have an npm account.
Versioning with semantic versioning
npm uses semantic versioning (semver). A version has three numbers, MAJOR.MINOR.PATCH, and each one makes a promise to consumers.
| Change | Bump | Example |
|---|---|---|
| Bug fix, no change to the public API | patch | 1.0.0 to 1.0.1 |
| New backward-compatible feature | minor | 1.0.1 to 1.1.0 |
| Breaking change | major | 1.1.0 to 2.0.0 |
Consumers usually install with a caret range such as ^1.0.0. That range accepts any 1.x.y release at or above 1.0.0, so a breaking change in a minor release breaks their builds without warning.
What counts as breaking in a TypeScript library
For a TypeScript library, the public API includes your types. Several changes break consumers even though the JavaScript behaves the same:
- Removing or renaming an exported function, type or interface.
- Making an optional property required, for example changing
separator?: stringtoseparator: stringinSlugOptions. - Narrowing a parameter type or widening a return type, such as changing
stringtostring | undefined. - Removing a path from the
exportsmap. A deep import that worked before now throwsERR_PACKAGE_PATH_NOT_EXPORTED. - Dropping a module format. Going from dual ESM and CommonJS to ESM-only breaks every
require()consumer. - Raising the minimum supported TypeScript or Node.js version.
These changes are safe in a minor release: adding an optional property, adding a new export, and adding an overload.
Versions below 1.0.0
Under 0.x, semver gives no strong guarantees. npm's caret range treats ^0.2.3 as >=0.2.3 <0.3.0, so a minor bump in 0.x acts like a major one. If other projects depend on your package, move to 1.0.0 once the API settles.
Bumping the version
Do not edit the version field by hand. Use npm version, which updates package.json and package-lock.json, creates a commit, and creates a git tag named v plus the version.
npm version patch # 1.0.0 -> 1.0.1
npm version minor # 1.0.1 -> 1.1.0
npm version major # 1.1.0 -> 2.0.0The command refuses to run if your git working directory has uncommitted changes. This protects you from tagging a release that doesn't match what you built.
If you don't want the commit and tag, for example because CI creates them, pass --no-git-tag-version.
Pre-release versions and dist-tags
A pre-release lets testers try a change without moving everyone on the stable line. Create one with a pre-release identifier:
npm version preminor --preid=beta # 1.0.1 -> 1.1.0-beta.0
npm version prerelease # 1.1.0-beta.0 -> 1.1.0-beta.1npm assigns each published version to a distribution tag, called a dist-tag. The default tag is latest, and it is what npm install @acme/slugify resolves to. If you publish a pre-release without choosing a tag, it can become latest and reach every new installer. Always name the tag yourself:
npm publish --tag nextTesters then install with npm install @acme/slugify@next. Stable ranges such as ^1.0.0 never match pre-release versions, so existing consumers are not affected. When the pre-release is ready, publish the stable version normally. It takes the latest tag.
You can inspect and move tags later:
npm dist-tag ls @acme/slugify
npm dist-tag add @acme/[email protected] latestMoving latest back to a previous version is the fastest way to stop new installs from picking up a bad release.
Preparing the account and package
Before the first publish, check three things.
- You are logged in. Run
npm login, thennpm whoamito confirm the account. Turn on two-factor authentication for publishing in your npm account settings. - The name is yours. The scope
@acmemust be a user name or an organization you belong to. Check that the package name is free withnpm view @acme/slugify. A 404 error means the name is unused. - Access is set. Scoped packages are private by default, and private packages need a paid plan. The
publishConfigblock in the package.json from the earlier chapters handles this:
{
"publishConfig": {
"access": "public"
}
}Unscoped packages are always public, so they don't need this field. You can also pass --access public on the first command line instead.
The prepublishOnly script (npm run build) runs automatically before npm publish. It also runs before npm pack, so you get a fresh dist every time. This stops you from publishing stale or missing build output. It does not run your tests. If you want tests as a gate, add them to the script:
{
"scripts": {
"build": "tsc",
"test": "node --test",
"prepublishOnly": "npm test && npm run build"
}
}The release sequence
Putting versioning and the checks from the previous chapter together gives one repeatable sequence.
As commands:
git status # must be clean
npm run build
npm pack --dry-run
npx publint
npx @arethetypeswrong/cli --pack .
npm version minor
npm publish --dry-run # last rehearsal, uploads nothing
npm publish
git push --follow-tagsnpm publish --dry-run runs the whole publish process, including prepublishOnly, and prints the file list and version, but it uploads nothing.
If your account requires a one-time password, npm prompts for it. In a script, supply it with npm publish --otp=123456.
After publishing, confirm the registry sees what you expect:
npm view @acme/slugify version dist-tagsFinally, install the published version into your separate test consumer project and compile the consumer file from the earlier chapter. Test against the registry copy, not only the local tarball.
Publishing from CI with provenance
Publishing from your laptop ties releases to one person's machine and credentials. Many teams publish from CI instead and add provenance, a signed record linking the published package to the source commit and build that produced it. npm shows a provenance badge on the package page.
On GitHub Actions, the job needs permission to request an identity token:
name: release
on:
push:
tags: ["v*"]
permissions:
contents: read
id-token: write
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
registry-url: https://registry.npmjs.org
- run: npm ci
- run: npm publish --provenance
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}Use a granular access token limited to this package and to publishing, and store it as a repository secret. Never commit a token or put it in .npmrc in the repository.
When something goes wrong
A published version is permanent. npm does not let you republish the same version number, even after deleting it. This changes how you handle mistakes.
- A bad release. Publish a fixed patch version. If the bad version is
latest, move the tag back withnpm dist-tag adduntil the fix is ready. - A version nobody should use. Mark it with a warning that npm prints on install:
npm deprecate "@acme/[email protected]" "Broken type exports, use 1.1.1 or later"- An accidental secret in the package. Revoke the secret first. Then publish a clean version. Unpublishing does not undo exposure, because others may already have downloaded it. npm allows
npm unpublishonly under restricted conditions, such as shortly after publishing and with no dependents. Treat it as a last resort.
Keeping a changelog
Consumers decide whether to upgrade by reading your release notes. For each version, record what changed under headings such as Added, Changed, Fixed and Removed. Mark breaking changes clearly and state the migration step. A CHANGELOG.md at the package root is published automatically, because npm always includes README, LICENSE and CHANGELOG files regardless of the files field. Tools such as Changesets can generate version bumps and changelogs from small change files, if you want to automate this.
Exercise
Your package is at 1.4.2. You change SlugOptions so that separator is required instead of optional, then you want testers to try the change before everyone gets it. Which version number do you choose, and which commands publish it so that npm install @acme/slugify still installs the old API?
Show answer
Making an optional property required breaks existing callers who omit it, so this is a major change. Use npm version premajor --preid=beta, which gives 2.0.0-beta.0, then npm publish --tag next. The explicit next tag keeps latest on 1.4.2, and ^1.4.2 ranges never match pre-release versions. Testers install with npm install @acme/slugify@next.
Exercise
You published 1.5.0 and then noticed that its exports map points types at a file that is missing from the tarball. You can't republish 1.5.0. List the steps that limit the damage and fix the problem.
Show answer
First move latest back to the last good version with npm dist-tag add @acme/[email protected] latest. Then fix the files or exports configuration, run npm pack --dry-run and Are the Types Wrong to confirm the fix, and publish 1.5.1. Optionally run npm deprecate "@acme/[email protected]" "Missing type declarations, use 1.5.1" so installers of the broken version see a warning.