Skip to content

node-gyp configure 的时候 如何配置下载的镜像

删除 node-gyp 缓存文件(如果有的话)

powershell
cd ~\AppData\Local\node-gyp\
powershell
PS C:\Users\Administrator\AppData\Local\node-gyp> cd ~\AppData\Local\node-gyp\
PS C:\Users\Administrator\AppData\Local\node-gyp> ls


    Directory: C:\Users\Administrator\AppData\Local\node-gyp


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2024/9/18     22:31                Cache


PS C:\Users\Administrator\AppData\Local\node-gyp>

node-gyp configure

node-gyp 失败 gyp ERR! stack FetchError: request to https://nodejs.org/download/release/v18.14.0/node-v18.14.0-headers.tar.gz failed, reason: connect ETIMEDOUT 104.20.22.46:443,原因是网络原因

powershell
PS C:\Users\Administrator\Documents\mycode\electron-screenshot\screenshot> node-gyp configure
gyp info it worked if it ends with ok
gyp info using node-gyp@10.2.0
gyp info using node@18.14.0 | win32 | x64
gyp info find Python using Python version 3.9.12 found at "C:\Program Files\Python39\python.exe"

gyp http GET https://nodejs.org/download/release/v18.14.0/node-v18.14.0-headers.tar.gz
gyp http fetch GET https://nodejs.org/download/release/v18.14.0/node-v18.14.0-headers.tar.gz attempt 1 failed with ETIMEDOUT
gyp WARN install got an error, rolling back install
gyp ERR! configure error
gyp ERR! stack FetchError: request to https://nodejs.org/download/release/v18.14.0/node-v18.14.0-headers.tar.gz failed, reason: connect ETIMEDOUT 104.20.22.46:443
gyp ERR! stack at ClientRequest.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\node_modules\minipass-fetch\lib\index.js:130:14)
gyp ERR! stack at ClientRequest.emit (node:events:513:28)
gyp ERR! stack at _destroy (node:_http_client:883:13)
gyp ERR! stack at onSocketNT (node:_http_client:903:5)
gyp ERR! stack at process.processTicksAndRejections (node:internal/process/task_queues:83:21)
gyp ERR! System Windows_NT 10.0.22631
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\Administrator\\AppData\\Roaming\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "configure"
gyp ERR! cwd C:\Users\Administrator\Documents\mycode\electron-screenshot\screenshot
gyp ERR! node -v v18.14.0
gyp ERR! node-gyp -v v10.2.0
gyp ERR! not ok
PS C:\Users\Administrator\Documents\mycode\electron-screenshot\screenshot>

解决办法:通过命令行参数指定镜像

在执行 node-gyp configure 命令时,你也可以通过命令行直接传递参数来指定镜像:

bash
node-gyp configure --dist-url=https://npmmirror.com/mirrors/node

通过以上方法可以有效地加速 node-gyp 的依赖下载过程。