为什么

发布 npm 包除了发布到官方的 npm 仓库上或者自己搭建的私有 npm 仓库, 还有一种比较少见的, 就是发布到自己的 github 仓库上, 只有相关仓库或组织权限的成员才能下载该 npm 包,达到免费私有的目的

登录

npm login --registry=https://npm.pkg.github.com
  • username 小写
  • passwordtoken

初始化包

npm init
  • name 字段格式为@用户名小写/包名

指定包发布位置

{
  // ...
  "publishConfig": {
    "registry": "https://npm.pkg.github.com"
  },
  // ...
}

发布

npm publish

设置为公共可见

默认为私有, 进入 Packages - Package settings - Danger Zone - Change visibility 可以设置公共可见, 这样所有开发者都可以下载你的包

下载

npm install @reinerlau/test@1.0.0 --registry=https://npm.pkg.github.com

参考

https://github.com/ReinerLau/github-guide