第一次发布npm包遇到的一些坑,在这里记录一下
基本流程
初始化
npm init
登录npm
首先需要在npm上注册一个npm账号
npm login // 或 npm adduser
发布
npm publish
遇到的问题
1、密码输入问题
执行npm login后,需要输入用户名,密码,邮箱,都是注册npm时候的信息,到输入密码时发现怎么都输入不上,以为是卡了,一番搞,发现是密码被隐藏了
npm notice Log in on https://registry.npmjs.org/
Username: xxxxxx
Password:
Email: (this IS public) [email protected]
输入密码时在password:后正常输入密码回车即可,可以正常登陆。
此处导致我一直疑问的原因是输入密码时虽然被隐藏了,但是鼠标占位符没有随着输入字符儿移动,给人感觉没输入上任何字符的感觉
2、npm 源不对
npm ERR! code E403
npm ERR! 403 403 Forbidden - PUT https://registry.npmmirror.com/-/user/org.couchdb.user:xxxxxxxx - [FORBIDDEN] Public registration is not allowed
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.
原因:这个是你的源设置成第三方源的时候才有可能发生,比如设置了淘宝源就可能会导致该问题。只要把源改回默认的就可以了
解决方法:
- 全局安装nrm 也就是node的源管理器
// 终端或者cmd中输入
npm install -g nrm
// 安装好以后 输入
nrm use npm // 切换到npm官方的源地址
npm publish之后切换回来,正常安装包的时候可以使用nrm use taobao切换到淘宝镜像源
- 切换源
npm config set registry http://registry.npmjs.org
3、版本号重复
假设我们在一个已发布的包中,且包未修改的情况下,重复执行npm publish。会出现以下错误提示
npm ERR! code E403
npm ERR! 403 403 Forbidden - PUT https://registry.npmjs.org/liwe-ui - You cannot publish over the previously published versions: 0.1.0.
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.
原因:版本号未修改,与上次发布的版本一致
解决:更改package.json 配置:修改"version"属性值
4、无法发布私有包
npm ERR! code EPRIVATE
npm ERR! This package has been marked as private // 即只有移除掉该属性才能发布
npm ERR! Remove the 'private' field from the package.json to publish it.
原因:因为这是一个私有项目
解决:更改package.json 配置:修改 private 属性值为 false 即可
"private": false
此外,如果不想每次新建项目都手动设置,可以进行全局配置
$ npm config set init-private true