NPM镜像
推荐配置
查看当前镜像
1 | npm config list |
设置镜像
1 | npm config set registry https://registry.npmmirror.com/ |
其中npm config
提供的参数:
registry
是设置依赖的镜像disturl
是设置Node的镜像sass_binary_site
是sass的镜像electron_mirror
是electron的镜像python_mirror
是python的镜像
新版本除了registry
,其他都没法用命令设置了
我们可以编辑文件
1 | npm config edit |
添加
1 | registry=https://registry.npmmirror.com/ |
华为镜像
华为开源镜像站同时也提供了工具类加速镜像:
1 | npm config set registry https://mirrors.huaweicloud.com/repository/npm/ |
※ 设置nodejs工具的镜像地址
1 | npm config set disturl https://repo.huaweicloud.com/nodejs |
※ 设置Electron和Python的镜像地址
1 | npm config set electron_mirror https://repo.huaweicloud.com/electron/ |
※ 设置浏览器引擎驱动镜像地址
1 | npm config set phantomjs_cdnurl https://repo.huaweicloud.com/phantomjs |
※ 设置Node-Sass的镜像地址
1 | npm config set sass_binary_site https://repo.huaweicloud.com/node-sass |
淘宝镜像
1 | npm config set registry https://registry.npm.taobao.org/ |
Electron镜像
查看配置文件的位置
1 | npm config list |
可以查看到本机的userconfig
在哪,即.npmrc
文件在哪
比如我的
1 | userconfig C:\Users\Jian\.npmrc |
打开该文件 添加
1 | registry=https://registry.npmmirror.com |
仅适用于中国区客户
MacOS
1 | # macOS |
Windows
1 | # Windows |
cnpm
1 | npm install cnpm -g |
如果使用npm下载依赖的时候报错的话,我们可以使用cnpm
1 | cnpm install |
常见问题
sass安装报错
安装报错,版本不匹配
Node Sass could not find a binding for your current environment: Windows 64-bit with Node.js 12.x
更换版本即可"node-sass": "^4.12.0",
更换后
在项目目录下,分别执行一下两个命令:
1 | npm rebuild node-sass |
再执行
1 | npm update |
node-sass版本依赖关系
https://www.npmjs.com/package/node-sass
NodeJS | Supported node-sass version | Node Module |
---|---|---|
Node 19 | 8.0+ | 111 |
Node 18 | 8.0+ | 108 |
Node 17 | 7.0+, <8.0 | 102 |
Node 16 | 6.0+ | 93 |
Node 15 | 5.0+, <7.0 | 88 |
Node 14 | 4.14+ | 83 |
Node 13 | 4.13+, <5.0 | 79 |
Node 12 | 4.12+, <8.0 | 72 |
Node 11 | 4.10+, <5.0 | 67 |
Node 10 | 4.9+, <6.0 | 64 |
版本不匹配
问题描述:
不同依赖包之间的版本不匹配可能导致项目启动失败。
解决方法:
检查 package.json
文件中的依赖版本,确保所有依赖包的版本兼容。可以使用 npm outdated
命令查看哪些依赖包需要更新,然后使用 npm update
命令进行更新。
安装失败
问题描述:
在安装依赖包时,可能会因为网络问题或其他原因导致安装失败。
解决方法:
检查网络连接,确保能够访问 npm 仓库。
如果仍然无法安装,可以尝试更换 npm 源,
例如使用华为镜像源:
1 | npm config set registry https://mirrors.huaweicloud.com/repository/npm/ |
处理 Vue CLI 版本冲突
检查全局版本:
使用 vue --version
命令查看全局安装的 vue-cli
版本,使用 npx vue --version
命令查看项目中使用的版本。
如果不一致,可以使用 npm install -g @vue/cli
命令更新全局版本。
使用 npx
命令:
在项目中使用 npx vue-cli-service serve
命令启动项目,确保使用的是项目中安装的 vue-cli
版本。
处理依赖冲突
检查 package.json
文件:
确保 package.json
文件中的 vue
版本与项目中其他依赖包的版本兼容。
可以使用 npm update vue
命令更新 Vue.js 核心库版本。
锁定版本:
在 package.json
文件中不使用 ^
或 ~
符号,锁定 依赖 版本,避免因版本变化导致的问题。
安装失败
style-resources-loader@”1.3.3” from the root project
预处理器安装
根据不同预处理器(Sass/Less/Stylus),安装响应的webpack loader:
1 | # Sass |
安装style-resources-loader
1 | # 使用vue add安装 |