Scripts2026年7月8日·1 分钟阅读

np — A Better npm Publish with Safety Checks

np is an interactive CLI tool that automates the npm package publishing workflow with version bumping, test runs, git tagging, two-factor auth prompts, and safety checks.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Overview
直接安装命令
npx -y tokrepo@latest install b692b6f9-7a85-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

np wraps the entire npm publish workflow into a single interactive command. It runs your tests, bumps the version, creates a git tag, pushes to the remote, publishes to the npm registry, and creates a GitHub release draft. Each step includes safety checks to prevent common publishing mistakes like dirty working trees or skipped tests.

What np Does

  • Runs the full test suite before allowing a publish to proceed
  • Verifies the working directory is clean with no uncommitted changes
  • Bumps the version in package.json using SemVer (patch, minor, major, or custom)
  • Creates an annotated git tag and pushes it to the remote repository
  • Publishes the package to npm with optional two-factor authentication prompts

Architecture Overview

np orchestrates a sequential pipeline of checks and actions. It starts with prerequisite validation (clean git state, correct branch, npm auth), then runs the test script from package.json. After tests pass, it prompts for the version bump type, updates package.json and lockfiles, commits, tags, pushes to git, and finally runs npm publish. Each step is guarded by error handling that aborts the pipeline cleanly if anything fails, preventing partial publishes.

Self-Hosting & Configuration

  • Install globally with npm install -g np or run via npx np
  • Configure in package.json under the np key or in a .np-config.js file
  • Set "tests": false to skip test runs for projects without a test suite
  • Use "yarn": true to run Yarn instead of npm for installs and publishing
  • Set "branch": "main" to enforce publishing only from a specific branch

Key Features

  • Interactive version selection: choose patch, minor, major, prepatch, or custom
  • Prerequisite checks: clean git state, correct branch, valid npm auth, up-to-date dependencies
  • Two-factor auth: prompts for OTP when your npm account requires it
  • GitHub releases: optionally opens a pre-filled GitHub release draft after publishing
  • Monorepo support: works with workspaces when run from a package directory

Comparison with Similar Tools

  • npm publish — Raw publish command with no safety checks; np adds validation and automation around it
  • release-it — More configurable release pipeline; np is opinionated and requires less setup
  • semantic-release — Fully automated based on commit messages; np is interactive and developer-driven
  • Changesets — Designed for monorepo versioning workflows; np targets single-package publishing

FAQ

Q: Does np work with Yarn? A: Yes. Set "yarn": true in your np config and it will use Yarn for install and publish steps.

Q: Can I do a dry run? A: Yes. Run np --preview to see what version would be published and which steps would execute without actually running them.

Q: Does it support scoped packages? A: Yes. Scoped packages publish normally. For public scoped packages, ensure publishConfig.access is set to "public" in package.json.

Q: What happens if tests fail? A: np aborts the entire pipeline and no version bump, tag, or publish occurs.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产