使用最新的 node v12 安装 electron 时,发现每次跑到下面的语句就会挂住,无法继续安装:

> electron@7.1.7 postinstall /Users/cxf/Dev/CapsIndicator/node_modules/electron
> node install.js

查看网上相关解决方案,发现是因为 npm 需要连接 github 下载 electron 安装包导致,安装包有 60M 左右,但从 github 下载却只有几 KB,所以感觉是卡住了。但改使用淘宝的 cnpm 发现还是会卡住,这是因为 electron 的包默认还是指向 github 下载,还有 electron 提供了修改下载地址的环境变量electron_mirror,使用npm config设置配置变量

npm config set electron_mirror "https://npm.taobao.org/mirrors/electron/"

再次执行安装命令,虽然不会卡住了,但变为直接提示404安装失败:

> electron@7.1.7 postinstall /Users/cxf/Dev/CapsIndicator/node_modules/electron
> node install.js

(node:2216) UnhandledPromiseRejectionWarning: HTTPError: Response code 404 (Not Found)
    at EventEmitter.<anonymous> (/Users/cxf/Dev/CapsIndicator/node_modules/got/source/as-stream.js:35:24)
    at EventEmitter.emit (events.js:210:5)
    at module.exports (/Users/cxf/Dev/CapsIndicator/node_modules/got/source/get-response.js:22:10)
    at ClientRequest.handleResponse (/Users/cxf/Dev/CapsIndicator/node_modules/got/source/request-as-event-emitter.js:155:5)
    at Object.onceWrapper (events.js:300:26)
    at ClientRequest.emit (events.js:215:7)
    at ClientRequest.origin.emit (/Users/cxf/Dev/CapsIndicator/node_modules/@szmarczak/http-timer/source/index.js:37:11)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:583:27)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:115:17)
    at TLSSocket.socketOnData (_http_client.js:456:22)
(node:2216) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:2216) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
npm WARN capsindicator@1.0.0 license should be a valid SPDX license expression

查看 github issue#1050 后,发现是因为淘宝保存的版本目录和官方的不同,淘宝去掉了v前缀。

// 官方
https://npm.taobao.org/mirrors/electron/v7.1.7/electron-v7.1.7-linux-x64.zip
// 淘宝
https://npm.taobao.org/mirrors/electron/7.1.7/electron-v7.1.7-linux-x64.zip

好在 electron 官方也提供了修改目录名称的环境变量electron_custom_dir,配置改下:

npm config set electron_custom_dir "7.1.7"

再次运行安装命令,这次终于成功了😂

官方相关的安装说明

UTOOLS1577342703742.png