当你兴高采烈地下载完 Git 仓库中的项目,端起红茶,敲下 yarn
准备开发。不久后你就会发现满屏幕的 error :
这时候我就想起一句名言
有时生活就像石头,会给你迎头一击。 —— 乔布斯
面对国内网络状况,即使使用 Yarn 也不能快速地拉取依赖包,多数因为网络状态不佳,下载错误导致的构建失败:
[4/4] Building fresh packages...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
error \node_modules\node-sass: Command failed.
- 网络问题导致的 node-sass 下载失败
- 部分资源指向了托管在
s3.amazonaws.com
上的 github - 依赖库损坏,校检错误
删除项目冗余文件(开发环境)
删除依赖产生的错误配置和文件 node_modules/
package-lock.json
yarn.lock
方案一 创建 .npmrc 或 .yarnrc
如果在 CI/CD 上出现被墙问题,最好通过配置 .yarnrc
文件的方式,一步解决拉取依赖包的问题。
[4/4] Building fresh packages...
在项目根配置 .yarnrc
文件,文件会合并覆盖掉全局的配置,使用 yarn config list
可以查看当前的 Yarn 配置项。
registry "https://registry.npm.taobao.org"
sass_binary_site "https://npm.taobao.org/mirrors/node-sass/"
phantomjs_cdnurl "http://cnpmjs.org/downloads"
electron_mirror "https://npm.taobao.org/mirrors/electron/"
sqlite3_binary_host_mirror "https://foxgis.oss-cn-shanghai.aliyuncs.com/"
profiler_binary_host_mirror "https://npm.taobao.org/mirrors/node-inspector/"
chromedriver_cdnurl "https://cdn.npm.taobao.org/dist/chromedriver"
puppeteer_download_host "https://npm.taobao.org/mirrors"
gist - used yarn config to fix network connection err
方案二 设置淘宝 NPM 镜像
通过命令的方式设置本机的 Yarn 配置文件,将仓库源服务器指向淘宝提供的镜像:
yarn config set registry https://registry.npm.taobao.org -g
yarn config set sass_binary_site http://cdn.npm.taobao.org/dist/node-sass -g
方案三 使用代理
使用一个 HTTP/S 代理可以顺利地下载好依赖文件
yarn config set proxy http://127.0.0.1:23333
# 下载完成后删除 http 代理
yarn config delete proxy
The engine "node" is incompatible with this module
info [email protected]: The platform "win32" is incompatible with this module.
info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
error [email protected]: The engine "node" is incompatible with this module. Expected version ">=4 <=9". Got "10.15.1"
error Found incompatible module
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
yarn --ignore-engines
yarn config set ignore-engines true
参考资料
npm的.npmrc文件在哪里?缓存及全局包文件在什么位置?
registry "https://registry.npm.taobao.org/"
cache-folder "E:\\yarn"
disturl "https://npm.taobao.org/dist"
ignore-engine true
ignore-engines true
lastUpdateCheck 1602300083813
sass_binary_site "https://npm.taobao.org/mirrors/node-sass/"
phantomjs_cdnurl "https://npm.taobao.org/dist/phantomjs"
electron_mirror "https://npm.taobao.org/mirrors/electron/"
sqlite3_binary_host_mirror "https://foxgis.oss-cn-shanghai.aliyuncs.com/"
profiler_binary_host_mirror "https://npm.taobao.org/mirrors/node-inspector/"
chromedriver_cdnurl "https://cdn.npm.taobao.org/dist/chromedriver"
puppeteer_download_host "https://npm.taobao.org/mirrors"