展开目录
npm远程服务器某些配置不兼容代理的解决办法
npm
X
陈尼玛的博客
记录开发生涯的踩坑经历,用时间来验证成长
加载中

假设系统代理为 http://127.0.0.1:8082 ,需要从 http://registry.xx.com 这个仓库下载 xx 包。

则npm使用代理安装包的命令如下:

npm --proxy http://127.0.0.1:8082 --registry=http://registry.xx.com install xx

npm会在把请求头里的host设成 registry.xx.com:80,这时候远程服务器可能会解析失败返回503 service unavailable之类的错误,那么可以新增一个代理,把请求头部的host字段去掉端口号:

npmfix.js

// node npmfix 8000,127.0.0.1:8082

const [,, portstr='8000,127.0.0.1:8082']=process.argv
const [listen, proxyaddr, proxyport]=portstr.split(/,|:/)
const net=require('net')
console.log({listen, proxyaddr, proxyport})

net.createServer(c=>{
  const dst=net.createConnection(proxyport, proxyaddr)
  c.on('data', buf=>{
    const [str, rep, fix]=[buf.toString('utf8'), /registry.xx.com:80/, 'registry.xx.com']
    dst.write(str.match(rep)? str.replace(rep, fix): buf)
  })
  dst.pipe(c)
}).listen(listen)

此时运行 node npmfix 命令,再运行npm安装就可以了:

npm --proxy http://127.0.0.1:8000 --registry=http://registry.xx.com install xx

相关文档

  1. npm包命令行调用

  2. npm设置仓库地址和代理

  3. webrtc服务搭建

随便看看

  1. 记一次nodejs内存泄漏的排查经历

  2. 安卓文字偏上,文字顶部被遮罩

  3. sass变量和继承类写法

  4. ipsec vpn 添加新账号

  5. nodejs 长连接

  6. curl用法

  7. jxa运动指令脚本

  8. git删除远程分支

  9. python 用摄像头拍照并写入文件

  10. mongodb 批量修改字段语句

  11. 树莓派 3B/3B+ usb启动

  12. cdn资源列表

  13. python下载文件,带进度条控制

  14. mac搜索局域网内的主机

  15. 树莓派配置收发邮件

  16. bootstrap modal弹框导致ie无法获取焦点

  17. 配置mysql ssl连接

  18. 前端性能观察器

畅言模块加载中