Sub topic 13
Versioning and publishing to npm
Assume you know TypeScript, package.json, npm scripts, and the difference between ESM and CommonJS. This chapter focuses on releasing a package safely. It covers version numbers, pre-release checks, and the npm publish command.
Use semantic versioning
Semantic versioning, or SemVer, gives meaning to a package version in the form MAJOR.MINOR.PATCH.
- Increase
PATCHfor backward-compatible bug fixes, such as1.0.0to1.0.1. - Increase
MINORfor backward-compatible features, such as1.0.1to1.1.0. - Increase
MAJORfor breaking changes, such as1.1.0to2.0.0.
A breaking change requires consumers to change their code. Removing an exported function or changing a function parameter type is usually breaking. Adding an optional property is usually backward-compatible.
Keep the package version in package.json. Do not reuse a version after publishing it. npm treats a published package version as an immutable release.
Let npm update the version
The npm version command changes the version in package.json and creates a Git commit and tag by default.
npm version patch
npm version minor
npm version majorFor example, if the current version is 1.4.2, npm version minor changes it to 1.5.0. Review the generated commit and tag before pushing them:
git show --stat
git push origin main --follow-tagsYou can publish a pre-release when testing a future release:
npm version 2.0.0-beta.1
npm publish --tag nextConsumers must explicitly install the next tag. This keeps a beta release from becoming the default version.
Check the release before publishing
A release should contain the built files, declaration files, and only the public package contents. Run the build and inspect the tarball that npm would create.
npm run build
npm pack --dry-runnpm pack --dry-run lists the files selected for publication without creating or uploading a package. Check that dist contains the JavaScript and .d.ts files referenced by package.json. Check that tests, source maps you do not intend to ship, local configuration, and secrets are excluded.
Run package checks before changing the version or publishing:
npx publint
npx @arethetypeswrong/cli --pack .Then test the packed archive from a separate consumer project. This catches errors that a test inside the library can miss, such as an incorrect exports path or missing declaration file.
Publish a versioned package
Authenticate with the registry account that owns the package:
npm login
npm whoamiFor a public scoped package, publish with explicit access:
npm publish --access publicThe package's prepublishOnly script runs before publication. If it runs npm run build, publishing stops when the build fails.
The release process is one-way in normal use:
flowchart TD
A[Update code and tests] --> B[Choose SemVer version]
B --> C[Build package]
C --> D[Inspect npm pack dry run]
D --> E[Run publint and type checks]
E --> F[Publish version]
F --> G[Verify npm registry metadata]After publishing, verify the registry metadata:
npm view @acme/slugify version
npm view @acme/slugify dist-tagsUse npm deprecate to warn users about a problematic release. Avoid unpublishing except when npm policy permits it and there is a serious reason. A deprecation notice preserves dependency resolution while explaining what consumers should do.
Exercise
Exercise
You added a backward-compatible exported function to version 1.2.3. Which version should you publish, and which command creates it?
Show answer
Publish 1.3.0. Run npm version minor, review the generated commit and tag, then run the release checks and npm publish.