github发布公开npm包
钢翼
编程
对于我们自己在github上公开的前端项目,一般我们可以直接通过以下方法来进行安装,但是这样并不能很好的控制版本。
npm install {github用户名}/{github仓库名}
使用npmjs又觉得注册麻烦。
所以我们可以使用github packages来发布我们的npm包。
1.修改package.json
- 去除
private字段 - 修改
name,必须是@{github用户名}/{包名} - 添加
repository - 添加
publishConfig - 添加
version版本号 - 所有第三方依赖包应安装到
devDependencies中!!!
{
"name":"@github_user/npm_module_name",
"version:"0.0.1",
"repository": {
"type": "git",
"url": "<git_repo_link>"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com/<github_user>"
},
......
}
2.编辑发布忽略文件
一般我们的包只需要包含dist的文件
在项目根目录新增 .npmignore,配置忽略文件和文件夹
.idea
.DS_Store
package-lock.json
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
test
tools
docs
miniprogram_dev
node_modules
coverage
public
.vscode
src
index.html
build
3.npm登录github并发布
前期准备,在 https://github.com/settings/tokens 已经创建了有项目权限的token
# 登录,执行后输入用户名,token,邮箱
npm login --registry=https://npm.pkg.github.com
# 项目打包
npm run build
# 项目发布(确保版本号不冲突)
npm publish
默认发布的github packages是私有的,需要在packages setting将其设置为公开。
4.安装我们的包
npm install @{github用户名}/{包名}@{版本号} --registry=https://npm.pkg.github.com
# 或
yarn add @{github用户名}/{包名}@{版本号} --registry=https://npm.pkg.github.com