从npm ERR! code E404到顺畅安装插件:你的VSCode安装障碍一扫光!
2023-02-26 15:26:26
在vscode安装插件时解决npm ERR! code E404报错的两种方案
身为码农,使用各种插件来提升开发效率已经成为家常便饭。然而,最近我在配置commit代码提交规范时,却在安装commitlint插件时遇到了一个恼人的问题——npm ERR! code E404。不要着急,本文将为你提供两种行之有效的解决方案,助你轻松解决这一难题。
报错原因
在命令行中运行npm install -g commitlint
命令安装commitlint插件时,出现报错信息:
npm ERR! code E404
npm ERR! 404 Not Found: commitlint@9.3.0
npm ERR! 404
npm ERR! 404 'commitlint@9.3.0' 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默认从国外的npm仓库下载插件,而国内网络环境不稳定,导致无法顺利下载插件。
解决方案
方案一:使用淘宝镜像
淘宝镜像是一个国内的npm镜像,我们可以通过它来安装commitlint插件。具体步骤如下:
- 在命令行中运行以下命令:
npm config set registry https://registry.npm.taobao.org
- 再次运行
npm install -g commitlint
命令,即可成功安装commitlint插件。
方案二:使用cnpm
cnpm是一个国内的npm客户端,它可以自动从淘宝镜像安装插件。具体步骤如下:
- 安装cnpm:
npm install -g cnpm --registry=https://registry.npm.taobao.org
- 使用cnpm安装commitlint插件:
cnpm install -g commitlint
代码示例
以下是如何使用两种方案安装commitlint插件的代码示例:
方案一:使用淘宝镜像
npm config set registry https://registry.npm.taobao.org
npm install -g commitlint
方案二:使用cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install -g commitlint
常见问题解答
1. 安装commitlint插件后,为什么在vscode中找不到?
需要重启vscode才能生效。
2. 淘宝镜像和cnpm有什么区别?
淘宝镜像是一个npm镜像,而cnpm是一个npm客户端。淘宝镜像可以加速下载速度,而cnpm可以自动从淘宝镜像安装插件。
3. 除了淘宝镜像和cnpm,还有其他解决办法吗?
可以手动下载commitlint插件的tar包,然后使用npm install -g
命令安装。
4. 为什么npm ERR! code E404错误只在国内网络环境中出现?
因为国外npm仓库无法在中国大陆地区稳定访问。
5. 使用淘宝镜像或cnpm后,还需要修改npm的registry吗?
不需要,淘宝镜像和cnpm会自动将npm的registry设置为淘宝镜像的地址。