发布一个npm包
我们每天都要安装各种npm依赖,那npm是怎么发布到远程仓库中的?
下面展示一个包发布到npm中的方法。
构建一个 npm 包
1 | npm init |
初始化参数
name: 项目的名称,确保在npm包里是独一无二的
version: 版本号默认1.0.0
description: 包描述
entry point: 包主入口文件,默认index.js
test command: 测试命令
git repository: git远程地址
keywords: 关键词
author: 作者
license: MIT
添加 README.md
1 | xxxxx |
注册 npm 账号
- npm官网
- 记住自己的账号和密码(之后需要)
下面的操作请确保你的node没有配置淘宝或其他镜像地址,否则执行 npm config set registry https://registry.npmjs.org/ 将源调整回来
1 | npm adduser |
1 | username: |
发布 npm
1 | npm publish --access=public |
- 进入官网查找你的包
引入 npm 包
1 | # 普通的安装方法(commonjs规范) |
使用 unpkg 引用 npm 中的包
1 | unpkg.com/:package@:version/:file |
评论
TwikooWaline


