记录一次nexus私服使用npm的坑(`npm publish`报400、401)

记录一次nexus私服使用npm的坑(`npm publish`报400)

400 Bad Request

  1. 准备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. 准备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
    ...
    
  3. 发布(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需满足:

    1. 勾选online属性
    2. 创建时的类型(type)选择为hosted

    我上面报错是因为制定了group类型的仓库地址,所以存在问题
    在这里插入图片描述


解决办法:

  1. 第一步:指定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/

    注意:

    1. login时需要指定registry,且为hosted类型,不能public
    2. 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安装验证,看看能否正常调用


总结:

  1. npm publish发布到私服,需要指定registryhosted私服仓库地址
  2. npm publish发布之前需要先使用npm login进行登录
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页