• 认真地记录技术中遇到的坑!
  • 能摸鱼真是太好啦!嘿嘿嘿!

npm安装依赖包出错问题处理

JavaScript 悠悠 6年前 (2018-09-25) 30732次浏览 3个评论

本文记录在使用npm安装依赖包过程中踩过的坑。一般来说,npm安装失败时需要注意下面几点:

  • 是否能够访问安装源:检查代理和当前安装源
  • 当前的node版本是否兼容已有模块node_modules
  • 输入的依赖包名称是否正确

npm ERR! code ECONNRESET

在使用npm安装node的模块的时候,经常会出现下面的一些错误:

  • npm ERR! network tunneling socket could not be established, cause=connect ECONNREFUSED
  • npm ERR! code ECONNRESET
  • npm ERR! code ETIMEOUT
  • npm ERR! code ENOFFOUND

这些错误的原因很有可能是npm使用默认的源下载安装包,而默认的安装源是国外网站,国内访问不了无法获取依赖包信息。

这时只需要更换为国内的安装源即可,可在命令行更换为国内淘宝的源:

# 查看自己的安装源
npm config get registry

# 更换npm源为国内淘宝镜像
npm config set registry http://registry.npm.taobao.org/

# 或者更换为国内npm官方镜像
npm config set registry http://registry.cnpmjs.org/

# 还原npm源
npm config set registry https://registry.npmjs.org/

npm安装源设置

上面介绍了通过命令行设置安装源地址的办法,另外还可以设置代理,以及编辑配置文件等方法。

# 命令行设置代理
npm config set proxy="http://127.0.0.1:1034"

# 还可以直接使用npm设置,不需要config
npm --registry https://registry.npm.taobao.org info underscore

另外还可以直接编辑npm安装配置文件:.npmrc,在文件末尾添加两行:

registry="http://registry.npmjs.org"

 proxy="http://127.0.0.1:1034"

这个文件一般在用户目录或者安装目录下。

SSL Error: CERT_UNTRUSTED

这个是因为HTTPS的设置问题,可以有两种办法解决:

  • 关掉SSL检测
  • 使用HTTP连接的安装源

命令如下:

# 关闭SSL检查
npm config set strict-ssl false

# 使用http安装源
npm config set registry="http://registry.npmjs.org/"

npm WARN unmet dependency

这个错误的原因很有可能是按照依赖包的过程重网络超时等导致,可以通过清空node_module修复:

# 删除node_modules目录
rm -rf node_modules/

# 情况缓存
npm cache clean

# 重新安装
npm install

有时候,因为node版本过低也会导致该问题,可以使用下面命令更新node

npm update -g npm

npm ERR! code EINTEGRITY

npm install时报错:
npm ERR! code EINTEGRITY
npm ERR! sha512- sha512-rkIa1OSVWTt4g9leLSK/PsqOj3HZbDKHbZj

这个问题有可能是npm版本过低导致,需要更新npm版本:

# 更新npm
npm install -g npm

# 继续安装
npm install

npm ERR! code EPERM

安装时出现:

npm ERR! code EPERM
npm ERR! errno -4048

这个错误出现的原因很多,其中一个是因为npm安装缓存的问题,可以尝试执行下面命令清空缓存。

npm cache clean --force

喜欢 (46)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(3)个小伙伴在吐槽
  1. 帮到了
    男巫2021-08-30 12:40 回复
  2. 求解决,困住我一天了 网上的方法都试过了不行更改镜像源什么的 npm ERR! code E404 npm ERR! 404 Not Found - GET https ://registry.npmjs.com/caihrc - Not found npm ERR! 404 npm ERR! 404 'caihrc@1.0.2' is not in the npm registry. npm ERR! 404 You should bug the author to publish it (or use the name yourself!) npm ERR! 404 npm ERR! 404 Note that you can also install from a npm ERR! 404 tarball, folder, http url, or git url. npm ERR! A complete log of this run can be found in: npm ERR! C:\node\node_cache\_logs\2021-04-22T09_10_58_598Z-debug.log
    张**2021-04-22 17:16 回复
  3. npm ERR! code E400这是什么情况
    2021-03-20 12:20 回复