解决npm安装electron失败的问题

1.问题描述

使用npm创建项目后,添加electron依赖
npm install electron --save-dev
一直报错,部分报错日志如下
484 silly audit error [object Object]
485 silly audit report null
486 info run electron@31.2.1 postinstall { code: 1, signal: null }
487 verbose stack Error: command failed
487 verbose stack     at promiseSpawn (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\promise-spawn\lib\index.js:22:22)
487 verbose stack     at spawnWithShell (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\promise-spawn\lib\index.js:124:10)
487 verbose stack     at promiseSpawn (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\promise-spawn\lib\index.js:12:12)
487 verbose stack     at runScriptPkg (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\run-script\lib\run-script-pkg.js:77:13)
487 verbose stack     at runScript (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\run-script\lib\run-script.js:9:12)
487 verbose stack     at C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\rebuild.js:329:17
487 verbose stack     at run (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\promise-call-limit\dist\commonjs\index.js:67:22)
487 verbose stack     at C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\promise-call-limit\dist\commonjs\index.js:84:9
487 verbose stack     at new Promise (<anonymous>)
487 verbose stack     at callLimit (C:\Users\Administrator\AppData\Roaming\npm\node_modules\npm\node_modules\promise-call-limit\dist\commonjs\index.js:35:69)
488 verbose pkgid electron@31.2.1
489 error code 1
490 error path C:\Workspace\VsCode\electron\papercup\node_modules\electron
491 error command failed
492 error command C:\WINDOWS\system32\cmd.exe /d /s /c node install.js
493 error RequestError: connect ETIMEDOUT 140.82.121.4:443
493 error     at ClientRequest.<anonymous> (C:\Workspace\VsCode\electron\papercup\node_modules\got\dist\source\core\index.js:970:111)
493 error     at Object.onceWrapper (node:events:634:26)
493 error     at ClientRequest.emit (node:events:531:35)
493 error     at origin.emit (C:\Workspace\VsCode\electron\papercup\node_modules\@szmarczak\http-timer\dist\source\index.js:43:20)
493 error     at TLSSocket.socketErrorListener (node:_http_client:500:9)
493 error     at TLSSocket.emit (node:events:519:28)
493 error     at emitErrorNT (node:internal/streams/destroy:169:8)
493 error     at emitErrorCloseNT (node:internal/streams/destroy:128:3)
493 error     at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
493 error     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1606:16)
494 silly unfinished npm timer reify 1721836281026
495 silly unfinished npm timer reify:build 1721836281989
496 silly unfinished npm timer build 1721836281989
497 silly unfinished npm timer build:deps 1721836281990
498 silly unfinished npm timer build:run:postinstall 1721836282003
499 silly unfinished npm timer build:run:postinstall:node_modules/electron 1721836282003
500 verbose cwd C:\Workspace\VsCode\electron\papercup
501 verbose os Windows_NT 10.0.22631
502 verbose node v20.15.1
503 verbose npm  v10.8.2
504 verbose exit 1
505 verbose code 1
506 error A complete log of this run can be found in: C:\Users\Administrator\AppData\Local\npm-cache\_logs\2024-07-24T15_51_20_629Z-debug-0.log

2.问题分析

根据日志以及网络查找到的信息,分析原因为安装electron时,npm会下载一个非常大的二进制文件,再加上网络不好最终导致安装失败

3.开始解决

尝试过的方法包括不限于
  1.  更换npm镜像源
    npm config set registry="https://registry.npmmirror.com"

     

  2.  设置环境变量
    ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/</code>

     

  3.  使用cnpm
    npm install -g cnpm --registry=https://registry.npmmirror.com
    cnpm install electron --save-dev

    最终使用第三种方法成功解决问题

posted @ 2024-07-25 17:29  赵米饭  阅读(1011)  评论(0编辑  收藏  举报