A better `npm publish`
np is a command-line tool that replaces the manual npm publish workflow with an automated, interactive process. Instead of manually running npm version, npm publish, git push, and git push --tags, np handles the entire release sequence in a single command. It verifies your working directory is clean, ensures you're on the correct branch, runs tests, publishes to npm, pushes Git commits and tags, and even creates GitHub release drafts.
The tool exists because manual publishing is error-prone. Developers commonly forget to run tests before publishing, push to the wrong branch, or forget to tag releases. np enforces best practices by default: it won't publish from a dirty working directory, prevents publishing from non-release branches, and automatically rolls back changes if the process fails. With over 150,000 weekly downloads, it's widely adopted by library maintainers who want a zero-configuration release workflow.
The package is particularly valuable for solo maintainers and small teams managing multiple npm packages. Rather than maintaining custom release scripts or remembering a dozen manual steps, developers run npx np, select a version bump (patch/minor/major), and let the tool handle the rest. It supports advanced scenarios like publishing from subdirectories, custom test scripts, dist-tags for beta releases, and selective feature disabling for unusual workflows.
np is opinionated by design. It assumes you're using Git, following semantic versioning, and want guardrails around the publishing process. If you need CI-driven automated releases based on commit messages, or require highly customized workflows, other tools may fit better. But for interactive local publishing with maximum safety and minimal configuration, np is the industry standard.
#!/usr/bin/env node
// Basic usage: npx np (interactive version selection)
// Programmatic example showing package.json configuration
// package.json setup for np with custom test script
{
"name": "my-library",
"version": "1.2.3",
"scripts": {
"test": "vitest run",
"publish-test": "npm pack && tar -xvzf *.tgz && node package/dist/index.js"
},
"devDependencies": {
"np": "^11.0.2"
},
"np": {
"testScript": "publish-test",
"2fa": true,
"branch": "main"
}
}
// Command line examples:
// Standard patch release (1.2.3 → 1.2.4)
// $ npx np patch
// Minor release with beta tag (1.2.3 → 1.3.0-beta.0)
// $ npx np minor --tag=beta
// Publish from dist/ subdirectory after build
// $ npm run build && npx np --contents=dist
// Preview release without making changes
// $ npx np --preview
// Skip tests and cleanup (use with caution)
// $ npx np patch --yolo
// Publish without creating GitHub release draft
// $ npx np --no-release-draft
// Custom branch and version selection
// $ npx np 2.0.0 --branch=next --tag=canaryLibrary Maintainer Publishing: An open-source maintainer needs to release a patch for a popular utility library. Running npx np patch automatically runs tests, bumps the version to 2.3.1, publishes to npm, commits the version change, pushes to GitHub, creates a release draft, and tags the commit—all in 30 seconds with built-in confirmation prompts.
Beta Release Testing: A team wants to test breaking changes with early adopters before a major release. Using npx np --tag=beta, they publish version 3.0.0-beta.1 under the 'beta' dist-tag, allowing users to opt-in via npm install package@beta while keeping the stable version as default.
Monorepo Package Publishing: A developer maintains a monorepo where the publishable package lives in a dist/ subdirectory after compilation. Using npx np --contents=dist, they publish only the compiled output without exposing source files or development tooling to npm.
Preview Mode for New Contributors: A first-time contributor wants to understand the release process without actually publishing. Running npx np --preview shows exactly what would happen—version bump, test execution, publish command, Git operations—without making any changes, serving as both documentation and validation.
Skip CI for Documentation Fixes: After fixing a typo in the README, a maintainer runs npx np patch --yolo to skip tests and cleanup for a trivial change. While not recommended for code changes, this speeds up documentation-only releases where test failures are impossible.
npm install nppnpm add npbun add np