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---
    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 发布到私服,需要指定 registry hosted私服仓库地址
  2. npm publish 发布之前需要先使用 npm login 进行登录
E 400 npm ERR! 400 Bad Request - PUT https://registry. npm js.org/xxx - Cannot publish over previously publish ed version "1.0.0". 由于我也是第 一次 做这种版本兼容性的插件,不少理解可能有误,欢迎指出。从这次的开发中,我意识到自己对 Vue 底层源码理解不够深刻,接下来的时间我打算在这方面进行学习,一起进步。 npm ERR! publish Failed PUT 401 npm ERR! code E 401 npm ERR! 404 [unauthorized] Login first: rgb_sj npm ERR! 404 npm ERR! 404 'rgb_sj' is not in th npm ERR! 404 You should bug the authe name yourself!) npm ERR! 404 npm ERR! 404 Note t 当执行 npm install 的时候就会返回 400 或者404错误,如下解决方法:1.查看 npm 配置文件 是否有错误:执行 npm config edit 查看配置文件信息执行 npm rebuild node-sass npm update npm install -g npm 升级 npm npm -v2. 重新安装nodejs... 用 nexus 搭建的 npm 本地仓库,在发布时 npm ERR!codeE 401 ,原因是没有基础权限,需要登录,用帐号登录时又 E500错误,从网上查了一下,资源比较少,而且基本重复,现把问题 记录 一下,以方便后期有相同问题的伙伴作参考 npm E 401 npm publish 时,错误详情如下: npm ERR! code E 401 npm ERR! Unable to authenticate, need: BASIC realm="Sonatype Nexus Repository Manag... 写了个eslint插件想发布到 npm 上,执行 npm publish , 错了: 原因是没登录 npm ,其实原因很简单,想发布个东西总得让人家知道你是谁吧,好的,现在解决问题只需要两步。 从淘宝源切到 npm 源, 记得之后要切回淘宝源 npm install -g nrm 或者用这个方法: npm config set registry=http://registry. npm js.org ... 1、排查镜像源,以及发布包版本号,一切正常; 2、仔细看看 错信息提及“opt”,我就怀疑是 npm 双重认证方面出现问题,但是百度很久也没有找到解决方法,知道看到这个博客https://juejin.cn/post/7131371977536700453 3、最终解决方法: (1)在 npm 官网上登录自己账号,然后点击Account -> Modift npm publish 日志中出现一下错误: verbose stack Error: 400 Bad Request - PUT http://xxxxx/repository/ npm -public/xxxx 19 verbose stack at D:\download\nodejs\node_global\node_modules\ npm \node_modules\ npm -registry-fetch\check-response.js:117:15 19 verbose stack