npm-publish
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesenpm 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 whoamiIf not logged in:
bash
npm login发布前,请确保你已登录npm。
bash
npm whoami如果未登录:
bash
npm login2. Preparation & Configuration
2. 准备与配置
Critical package.json
Fields
package.json关键的package.json
字段
package.jsonEnsure these fields are correct:
- name: Unique package name (scoped names like are recommended for organizations).
@org/pkg - 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 (or missing) to publish.
false
确保以下字段配置正确:
- name:唯一的包名(对于组织用户,推荐使用这类作用域名)。
@org/pkg - version:符合SemVer规范的版本号(例如)。
1.0.0 - main/module/exports:库的入口文件配置。
- files:要包含的文件白名单(可减小包体积)。
- private:必须设为(或者不配置该字段)才能公开发布。
false
Excluding Files
排除文件
Use a file or the array in to prevent publishing unnecessary files (tests, src, config files).
Tip: shows exactly what will be packed.
.npmignorefilespackage.jsonnpm publish --dry-run使用文件或者中的数组,避免发布不必要的文件(测试文件、源码、配置文件等)。
提示:命令可以准确展示将会被打包的内容。
.npmignorepackage.jsonfilesnpm publish --dry-runBuild (If applicable)
构建(如适用)
If your package requires compilation (TypeScript, Babel, etc.), run the build script first.
bash
npm run build如果你的包需要编译(TypeScript、Babel等),请先执行构建脚本。
bash
npm run build3. Versioning
3. 版本管理
Update the package version before publishing. This command increments the version in and creates a git tag.
package.jsonbash
undefined发布前先更新包版本。以下命令会自动更新中的版本号并创建git标签。
package.jsonbash
undefinedChoose 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
undefinednpm 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
undefined4. Publishing
4. 发布
Dry Run
试运行
Always do a dry run first to verify contents.
bash
npm publish --dry-run发布前务必先执行试运行来验证打包内容。
bash
npm publish --dry-runScoped Packages
作用域包
If publishing a scoped package (e.g., ) publicly for the first time:
@myorg/my-pkgbash
npm publish --access public如果是首次公开发布作用域包(例如):
@myorg/my-pkgbash
npm publish --access publicStandard Publish
标准发布
bash
npm publishbash
npm publish5. 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-tagsVerification
验证
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