记录一次nexus私服使用npm的坑(`npm publish`报400)
400 Bad Request
-
准备1:
准备nexus私服(略,网上有许多搭建nexus私服的教程,我这里使用了nexus3,作为自己的maven、npm私服)nexus repositories: 1. hosted:http://192.168.80.128:18081/repository/npm-local/ 2. proxy: http://192.168.80.128:18081/repository/npm-proxy-taobao/ 代理-淘宝npm仓库 (remote stage : https://registry.npm.taobao.org/) 4. group: http://192.168.80.128:18081/repository/npm-public/
-
准备2:
准备打包js包---cmd--- D: cd D:\Workspace\demo mkdir test-giannuo-npm-publish cd test-giannuo-npm-publish cd.>index.js // 随便写入一些js代码(hello world) npm init -y // 初始化package.json ...
-
发布(npm publish)
下面是一次试坑记录D:\Workspace\demo\test-giannuo-npm-publish>npm publish npm notice npm notice package: test-giannuo-npm-publish@0.0.1 npm notice === Tarball Contents === npm notice 88B index.js npm notice 328B package.json npm notice === Tarball Details === npm notice name: test-giannuo-npm-publish npm notice version: 0.0.1 npm notice package size: 383 B npm notice unpacked size: 416 B npm notice shasum: 97045fd063fb9790c0f286b884956a0b6a08c770 npm notice integrity: sha512-pn1/CC1FtVXVS[...]JtLEtuteKTrhw== npm notice total files: 2 npm notice npm ERR! code E400 npm ERR! 400 Bad Request - PUT http://192.168.80.128:18081/repository/npm-group/
错误原因:
npm publish
命令发布包到私服,需要指定registry
属性,为目标仓库地址,这里的仓库需要为hosted
类型的npm repository
,如下:
注意:
这里的npm-local
需满足:- 勾选online属性
- 创建时的类型(type)选择为
hosted
我上面报错是因为制定了group类型的仓库地址,所以存在问题
解决办法:
-
第一步:指定registry
可以在命令行中
npm publish --registry=http://192.168.80.128:18081/repository/npm-local/
或者
在package.json
文件下添加publishConfig
属性"publishConfig": { "registry": "http://192.168.80.128:18081/repository/npm-local/" }
这时,再运行
npm publish
会报401,因为没有登录npm notice npm notice package: test-giannuo-npm-publish@0.0.1 npm notice === Tarball Contents === npm notice 56B index.js npm notice 373B package.json npm notice === Tarball Details === npm notice name: test-npm-giannuo-publish npm notice version: 0.0.4 npm notice package size: 1.4 kB npm notice unpacked size: 4.7 kB npm notice shasum: baed4ef13354352e5385cecc2887efd422ab09f8 npm notice integrity: sha512-y/3c1x3Hr1qHS[...]Pn+kRbt5vu0yw== npm notice total files: 2 npm notice npm ERR! code E401 npm ERR! Unable to authenticate, need: BASIC realm="Sonatype Nexus Repository Manager"
使用
npm login
进行登录,同样,指定目标registry
npm login --registry=http://192.168.80.128:18081/repository/npm-local/
注意:
- login时需要指定
registry
,且为hosted
类型,不能是public
- registry地址最后的斜杠(
/
)不能省略
登录之后
Logged in as giannuo on http://192.168.80.128:18081/repository/npm-local/.
再使用
npm publish
> npm publish npm notice npm notice package: test-npm-giannuo-publish@0.0.4 npm notice === Tarball Contents === npm notice 56B index.js npm notice 373B package.json npm notice === Tarball Details === npm notice name: test-npm-giannuo-publish npm notice version: 0.0.4 npm notice package size: 390 B npm notice unpacked size: 429 B npm notice shasum: a258e19c465981ea02eee88975631e16e4c7c335 npm notice integrity: sha512-7LmrrU1IxfaTO[...]SiWgyhYhVqXrg== npm notice total files: 2 npm notice + test-npm-giannuo-publish@0.0.4
成功
使用npm install test-npm-giannuo-publish@0.0.4
安装验证,看看能否正常调用 - login时需要指定
总结:
npm publish
发布到私服,需要指定registry
为hosted私服仓库地址
npm publish
发布之前需要先使用npm login
进行登录