点击上方"蓝色字体"关注我,选择"设为星标"!
回复"电子书"领取超多经典计算机书籍
问题一
问题描述:
输入命令 pnpm add electron
后, electron
包在执行 postinstall
脚本时,尝试从网络上下载 Electron
二进制文件,但由于网络问题(如连接超时或代理设置问题),导致下载失败。
λ pnpm add electron
Packages: +75
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Progress: resolved 75, reused 75, downloaded 0, added 75, done
node_modules/.pnpm/electron@31.3.1/node_modules/electron: Running postinstall script, failed in 23.1s
.../node_modules/electron postinstall$ node install.js
│ RequestError: connect ETIMEDOUT 20.205.243.166:443
│ at ClientRequest.<anonymous> (G:\wokespace\dev\DesktopApp\electron\electron-app\node_modules\.pnpm\g...
│ at Object.onceWrapper (node:events:634:26)
│ at ClientRequest.emit (node:events:531:35)
│ at origin.emit (G:\wokespace\dev\DesktopApp\electron\electron-app\node_modules\.pnpm\@szmarczak+http...
│ at TLSSocket.socketErrorListener (node:_http_client:500:9)
│ at TLSSocket.emit (node:events:519:28)
│ at emitErrorNT (node:internal/streams/destroy:169:8)
│ at emitErrorCloseNT (node:internal/streams/destroy:128:3)
│ at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
│ at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1605:16)
└─ Failed in 23.1s at G:\wokespace\dev\DesktopApp\electron\electron-app\node_modules\.pnpm\electron@31.3.1\node_modules\electron
ELIFECYCLE Command failed with exit code 1.
解决方案:
设置 electron
镜像源,使用国内的镜像源来加速下载,尤其是在网络连接较慢或者有防火墙的情况下。
npm config set electron_mirror "https://npmmirror.com/package/electron/"
问题二
问题描述:在设置npm 配置时,报错
λ npm config set electron_mirror "https://npmmirror.com/package/electron/"
npm ERR! `electron_mirror` is not a valid npm option
解决方案:
执行 npm config list
命令,查看当前的配置信息。
λ npm config list
; "user" config from C:\Users\Administrator\.npmrc
//registry.npmjs.org/:_authToken = (protected)
cache = "G:\\packages\\npm\\cache"
home = "https://npmmirror.com"
prefix = "G:\\packages\\npm\\global"
registry = "https://registry.npmmirror.com/"
; node bin location = G:\nodejs\node.exe
; node version = v18.19.1
; npm local prefix = G:\wokespace\dev\DesktopApp\electron\electron-app
; npm version = 10.2.4
; cwd = G:\wokespace\dev\DesktopApp\electron\electron-app
; HOME = C:\Users\Administrator
; Run `npm config ls -l` to show all defaults.
找到这个文件 .npmrc
地址 ; "user" config from C:\Users\Administrator\.npmrc
, 然后打开新增一行:
electron_mirror=https://npmmirror.com/mirrors/electron/
可以通过 https://registry.npmmirror.com/binary.html
查看都有哪些二进制包
注意一定是 https://npmmirror.com/mirrors/${二进制包的名称}/
,比如 https://npmmirror.com/mirrors/node-sass/
问题三
问题描述:
输入命令 npm install electron --save-dev
后,出现以下错误。
λ npm install electron --save-dev
npm ERR! Cannot read properties of null (reading 'matches')
解决方案:
-
1.清理
npm
缓存λ npm cache clean --force
npm WARN using --force Recommended protections disabled.
-
2.验证并重建
npm缓存
λ npm cache verify
Cache verified and compressed (G:\packages\npm\cache_cacache) Content verified: 0 (0 bytes) Index entries: 0 Finished in 0.013s
-
3.直接删除
node_modules
目录,并再次运行npm install
:npm install --registry=https://registry.npmmirror.com