解决electron安装卡在node install.js和Response 404(Not Found)问题

解决electron安装卡在node install.js和Response 404(Not Found)问题

打算安装一下electron 8.0.2,运行了npm i -D electron@latest命令。

问题1:发现卡在> node install.js不动

参考网上的方法,给npm换源。

打开~/.npmrc,修改为

registry=https://registry.npm.taobao.org
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
phantomjs_cdnurl=http://npm.taobao.org/mirrors/phantomjs
electron_mirror=http://npm.taobao.org/mirrors/electron/

再次运行npm i -D electron@latest,不再卡顿

问题2:Response 404(Not Found)

解决了上一个问题出现,未能找到资源。

PS C:\Users\11244> npm i -D electron@latest

> [email protected] postinstall C:\Users\11244\node_modules\electron
> node install.js

(node:22132) UnhandledPromiseRejectionWarning: HTTPError: Response code 404 (Not Found) for http://npm.taobao.org/mirrors/electron/v8.0.2/electron-v8.0.2-win32-x64.zip
    at EventEmitter.<anonymous> (C:\Users\11244\node_modules\got\source\as-stream.js:35:24)
    at EventEmitter.emit (events.js:210:5)
    at module.exports (C:\Users\11244\node_modules\got\source\get-response.js:22:10)
    at ClientRequest.handleResponse (C:\Users\11244\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 (C:\Users\11244\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 Socket.socketOnData (_http_client.js:456:22)
(node:22132) 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:22132) [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 saveError ENOENT: no such file or directory, open 'C:\Users\11244\package.json'
npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\11244\package.json'
npm WARN 11244 No description
npm WARN 11244 No repository field.
npm WARN 11244 No README data
npm WARN 11244 No license field.

+ [email protected]
updated 1 package in 1.228s

原因:

Response 404的原因是,换源了之后,淘宝镜像的资源路径和官方的资源路径不同。淘宝的镜像路径是http://npm.taobao.org/mirrors/electron/8.0.2/,而electron官方默认的路径是http://npm.taobao.org/mirrors/electron/v8.0.2/,版本前面加了一个v.

解决方法:

可以修改变量值来修改默认路径。看看@electron/get中使用的url变量,由三部分组成:

url = ELECTRON_MIRROR + ELECTRON_CUSTOM_DIR + '/' + ELECTRON_CUSTOM_FILENAME

修改变量值

npm config set electron_mirror http://npm.taobao.org/mirrors/electron/
npm config set electron_custom_dir 8.0.2

完成

参考

https://blog.tri-lib.com/2019/12/fix-npm-hang-when-install-electron/

反思:

以后遇到这种配置相关的问题,可以直接从项目的github issue中寻找解决方法。

你可能感兴趣的:(各类配置)