npm发布流程

2,312 阅读1分钟
# 先设置再查看npm仓库地址
$ npm config set registry http://registry.npmjs.org/
$ npm config get registry
http://registry.npmjs.org/

查看npm的登录信息

$ npm whoami
npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in.
npm ERR! need auth You need to authorize this machine using `npm adduser`

登录npm仓库

$ npm login
Username: uuu
Password:
Email: (this IS public) uuu@host.com
Logged in as uuu on http://registry.npmjs.org/.

到此就是登录成功,如果没有账户先去注册
使用whoami再查询一次登录信息

$ npm whoami
uuu

如果需要创建如@xxx/yyy的仓库,需要去npm的Profile页面Organization中创建一个叫xxxscope
如果需要创建xxx仓库需要这个仓库没有在npm中重复
然后开始创建代码

$ mkdir yyy
$ cd yyy
# 如果需要创建带scope仓库
$ npm init -y --scope=xxx
# 如果需要创建不带scope仓库
$ npm init -y

完成后查看package.json查看name属性
带scope仓库变为了@xxx/yyy
不带scope仓库为yyy

# 创建一些代码进去
$ touch readme.md
$ echo "console.log("hello npm")" > index.js

代码准备好了,打开package.jsonversion修改为希望部署的版本号

# 以公有仓库部署使用
$ npm publish --access=public
# 以私有仓库部署使用
$ npm publish
npm notice
npm notice 📦  @xxx/yyy@1.0.0
npm notice === Tarball Contents ===
npm notice 82B  index.js
npm notice 210B package.json
npm notice 19B  readme.md
npm notice === Tarball Details ===
npm notice name:          @xxx/yyy
npm notice version:       1.0.0
npm notice package size:  332 B
npm notice unpacked size: 292 B
npm notice shasum:        fbf30958d3002505216f26bf28565333056dbcf3
npm notice integrity:     sha512-Po/cQPg7mJ+Yj[...]TYjS0gJQPSaYQ==
npm notice total files:   3
npm notice
+ @xxx/yyy@1.0.0

到此已经发布完成

如果发布失败提示无权限

npm ERR! code E403
npm ERR! 403 forbidden - PUT http://registry.host.com/@xxx%2fyyy - [no_perms] Private mode enable, only admin can publish this module
npm ERR! 403 In most cases, you or one of your dependencies are requesting
npm ERR! 403 a package version that is forbidden by your security policy, or
npm ERR! 403 on a server you do not have access to.

上面这种情况说明自己不在仓库的管理员列表中,需要将自己加入仓库管理员列表再重启npm服务

npm ERR! code E400
npm ERR! 400 Bad Request - PUT http://registry.host.com/@xxx%2fyyy - [maintainers_error] request body need maintainers

这说明package.json中没有maintainers字段,需要加上

"maintainers": ["uuu"]

就好了

npm ERR! code E403
npm ERR! 403 403 Forbidden - PUT http://registry.host.com/@xxx%2fyyy - [forbidden] uuu not authorized to modify @xxx/yyy, please contact maintainers: admin

这说明自己不再维护者列表中,需要通过

$ npm owner add uuu @xxx/yyy

将自己加入到管理员列表中

npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! [forbidden] uuu not authorized to modify @xxx/yyy, please contact maintainers: admin @xxx/yyy

npm ERR! A complete log of this run can be found in:

这说明自己不是这个仓库的维护人员,需要进行两步:

  1. 让管理员将自己添加到仓库的管理人员中
$ npm owner add uuu @xxx/yyy
  1. package.jsonmaintainers数组中添加自己的名字uuu,再次publish就好了

发布之后想删除一个版本

$ npm unpublish @xxx/yyy@1.0.0

发布之后想删除该仓库的所有版本

$ npm unpublish @xxx/yyy -f