**npm** 常用命令及打包发布

查看包的所有版本

npm view xxx versions

查看当前源地址

npm config get registry

修改镜像源

临时修改

npm --registry https://registry.npmmirror.com/ install express

全局修改

# 使用淘宝镜像
npm config set registry https://registry.npmmirror.com/

# 使用官方镜像
npm config set registry https://registry.npmjs.org/

当前项目修改

在当前项目配置 .npmrc 文件,例如:

registry=https://registry.npmmirror.com/
@ubt:registry=http://npm.edu.ubtrobot.com/

发包

以下内容使用公司 npm 私服作为案例来说明。

注册 adduser

npm adduser --registry http://npm.edu.ubtrobot.com

按照提示输入用户名、密码和邮箱。建议用户名直接使用你的公司邮箱。

登录 login

# 查看当前是否已登录
npm whoami

# 指定源登录
npm login --registry http://npm.edu.ubtrobot.com
# 或者
npm login --registry=http://npm.edu.ubtrobot.com

提交

git add .
git cz

npm 包地址和 git 仓库地址是两个概念,你的 git 仓库可以在 gerrit 上,也可以在 gitlab 上。

发布 publish

如果不是第一次发布,需要变更 package.json 中的 version,否则会提示版本已存在。

npm publish

注意!建议使用 npm 来代替 yarn 或者 pnpm 来执行发布命令

如果是向 npmjs 发 scope 包,需要先创建 scope,同时还需要将 access 设置为 public:

npm publish --access=public
# 或者
npm publish --access public

也可以通过配置package.json来实现:

"publishConfig": {
  "access": "public"
},

注意!publishConfig 权限非常高,甚至会覆盖命令行传递的参数。

指定源 --registry

npm publish --registry http://npm.edu.ubtrobot.com

成功后,在 http://npm.edu.ubtrobot.com/ 就能搜索到了。

删除 unpublish

npm unpublish 这个命令有的仓储支持,有的仓储不支持。

npm unpublish @fqht/ui@1.0.0 --force --registry https://registry.npmjs.org

注意!不要去 npmjs.com 的后台去删除,会删不干净,很麻烦。

如果删除不成功,可以尝试这个连接里面的方法。

废弃 deprecate

如果不能删除,可以使用命令 npm deprecate 来声明废弃包。

npm deprecate @fqht/ui@0.0.1 'test deprecate'

废弃的包除了安装时会有警示,并不影响被安装。

其他

如果你的发布 script 是 publish,yarn publish可能会导致命令冲突。

你不得不使用npm run publish去执行命令,建议命令使用 deploy 代替 publish。

常见问题

You should bug the author to publish it (or use the name yourself!)

© 2022  Arvin Xiang
Built with ❤️ by myself