npm-publish

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

npm Publish Guide

npm 发布指南

This skill guides you through the process of publishing a package to the npm registry.
本技能将引导你完成将包发布到npm registry的全流程。

1. Prerequisites

1. 前置条件

Before publishing, ensure you are logged in to npm.
bash
npm whoami
If not logged in:
bash
npm login
发布前,请确保你已登录npm。
bash
npm whoami
如果未登录:
bash
npm login

2. Preparation & Configuration

2. 准备与配置

Critical
package.json
Fields

关键的
package.json
字段

Ensure these fields are correct:
  • name: Unique package name (scoped names like
    @org/pkg
    are recommended for organizations).
  • version: SemVer compliant version (e.g.,
    1.0.0
    ).
  • main/module/exports: Entry points for your library.
  • files: Whitelist of files to include (reduces package size).
  • private: Must be
    false
    (or missing) to publish.
确保以下字段配置正确:
  • name:唯一的包名(对于组织用户,推荐使用
    @org/pkg
    这类作用域名)。
  • version:符合SemVer规范的版本号(例如
    1.0.0
    )。
  • main/module/exports:库的入口文件配置。
  • files:要包含的文件白名单(可减小包体积)。
  • private:必须设为
    false
    (或者不配置该字段)才能公开发布。

Excluding Files

排除文件

Use a
.npmignore
file or the
files
array in
package.json
to prevent publishing unnecessary files (tests, src, config files). Tip:
npm publish --dry-run
shows exactly what will be packed.
使用
.npmignore
文件或者
package.json
中的
files
数组,避免发布不必要的文件(测试文件、源码、配置文件等)。 提示
npm publish --dry-run
命令可以准确展示将会被打包的内容。

Build (If applicable)

构建(如适用)

If your package requires compilation (TypeScript, Babel, etc.), run the build script first.
bash
npm run build
如果你的包需要编译(TypeScript、Babel等),请先执行构建脚本。
bash
npm run build

3. Versioning

3. 版本管理

Update the package version before publishing. This command increments the version in
package.json
and creates a git tag.
bash
undefined
发布前先更新包版本。以下命令会自动更新
package.json
中的版本号并创建git标签。
bash
undefined

Choose one:

选择其一执行:

npm version patch # 1.0.0 -> 1.0.1 npm version minor # 1.0.0 -> 1.1.0 npm version major # 1.0.0 -> 2.0.0
undefined
npm version patch # 1.0.0 -> 1.0.1 npm version minor # 1.0.0 -> 1.1.0 npm version major # 1.0.0 -> 2.0.0
undefined

4. Publishing

4. 发布

Dry Run

试运行

Always do a dry run first to verify contents.
bash
npm publish --dry-run
发布前务必先执行试运行来验证打包内容。
bash
npm publish --dry-run

Scoped Packages

作用域包

If publishing a scoped package (e.g.,
@myorg/my-pkg
) publicly for the first time:
bash
npm publish --access public
如果是首次公开发布作用域包(例如
@myorg/my-pkg
):
bash
npm publish --access public

Standard Publish

标准发布

bash
npm publish
bash
npm publish

5. Post-Publish

5. 发布后操作

Push Tags

推送标签

Push the new version commit and tags to your git repository.
bash
git push --follow-tags
将新版本的提交和标签推送到你的git仓库。
bash
git push --follow-tags

Verification

验证

Check the npm registry or install the package in a test project to verify.
bash
npm view <package-name> version
查看npm registry或者在测试项目中安装该包来验证发布是否成功。
bash
npm view <package-name> version