Electron-packager 在 Windows 10 上尝试使用 CMD 将电子项目编译为 .exe 时返回 "Response code 404 (Not Found)"

Electron-packager returning "Response code 404 (Not Found)" when trying to compile electron project to .exe using CMD on Windows 10

我是一名初级开发人员,只是熟悉 electron 和 node.js。我正在尝试使用 electron-packager 包将我的 electron 项目转换为 .exe 文件,但每次尝试 returns 都会出现此错误:

Response code 404 (Not Found) for https://github.com/electron/electron/releases/download/v0.35.6/SHASUMS256.txt

我试过 URL,它 returns 错误 404,显然,从 "releases" 开始的任何内容都不存在。如果有帮助,我正在使用 Windows 10。

这是我的 main.js 文件:

    var app = require('app');  // Module to control application life.
var BrowserWindow = require('browser-window');  // Module to create native browser window.

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
var mainWindow = null;

// Quit when all windows are closed.
app.on('window-all-closed', function() {
    // On OS X it is common for applications and their menu bar
    // to stay active until the user quits explicitly with Cmd + Q
    if (process.platform != 'darwin') {
        app.quit();
    }
});

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
    // Create the browser window.
    mainWindow = new BrowserWindow({width: 900, height: 600});

    // and load the index.html of the app.
    mainWindow.loadURL("path to index.html");

    // Emitted when the window is closed.
    mainWindow.on('closed', function() {
        // Dereference the window object, usually you would store windows
        // in an array if your app supports multi windows, this is the time
        // when you should delete the corresponding element.
        mainWindow = null;
    });
});

("path to index.html"代替实际脚本中的真实路径)

这是 package.json 文件:

 {
  "name": "overboard",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "dependencies": {
    "electron-forge": "^5.2.4",
    "electron-packager": "^14.2.1",
    "pretty-bytes": "^2.0.1"
  },
  "devDependencies": {
    "electron-prebuilt": "^0.35.2"
  },
  "scripts": {
    "init": "npm install",
    "start": "electron main.js"
  },
  "author": "Me",
  "license": "ISC"
}

提前感谢您的帮助,非常感谢。

const { app, BrowserWindow } = require('electron');

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow = null;

// Quit when all windows are closed.
app.on("window-all-closed", function() {
    // On OS X it is common for applications and their menu bar
    // to stay active until the user quits explicitly with Cmd + Q
    if (process.platform != "darwin") {
        app.quit();
    }
});

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on("ready", function() {
    // Create the browser window.
    mainWindow = new BrowserWindow({ width: 900, height: 600 });

    // and load the index.html of the app.
    // Not sure where your index.html is placed but if you are using Electron-quick-starter then this will be right.
    // Plus mainWindow.loadURL("https://github.com") not for loading the local file but for loading the url at your browser.

    mainWindow.loadFile("./index.html"); 


    // Emitted when the window is closed.
    mainWindow.on("closed", function() {
        // Dereference the window object, usually you would store windows
        // in an array if your app supports multi windows, this is the time
        // when you should delete the corresponding element.
        mainWindow = null;
    });
});

electron-built 很久以前已重命名为 electron。 所以你应该更新你的 package.json

 {
  "name": "overboard",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "dependencies": {
    "pretty-bytes": "^2.0.1"
  },
  "devDependencies": {
    "electron": "latest",
    "electron-forge": "latest",
    "electron-packager": "latest",
  },
  "scripts": {
    "init": "npm install",
    "start": "electron ."
  },
  "author": "Me",
  "license": "ISC"
}

请删除您的 node_modulespackage-lock.json 并尝试 npm installnpm start

能够以不同的方式修复完全相同的错误。

尝试使用 GetStream 的 react-native-chat-tutorial 应用程序时出现错误 https://github.com/GetStream/react-native-chat-tutorial

cd react-native-chat-tutorial

yarn

[1/4]   Resolving packages...
[2/4]   Fetching packages...
[3/4]   Linking dependencies...
error <my-app-path>/react-native-chat-tutorial/node_modules/electron: Command failed.
Exit code: 1
Command: node install.js
Arguments: 
Directory: <my-app-path>/react-native-chat-tutorial/node_modules/electron
Output:
HTTPError: Response code 404 (Not Found) for https://github.com/electron/electron/releases/download/v9.4.3/electron-v9.4.3-darwin-arm64.zip
    at EventEmitter.<anonymous> 

发现在我的案例中,根本原因是 react-devtools 试图获取不适用于 Apple Silicon (M1) 芯片的 TODO。

从 GitHub https://github.com/facebook/react/issues/20454#issuecomment-888992424

上的这条有用评论中获得了一些肮脏的解决方法指导

最终对我有用的是在 package.json

中更改 react-devtools 的版本
FROM
"react-devtools": "^4.10.1",
TO
"react-devtools": "4.22.1",

因为那是当时的最新版本https://www.npmjs.com/package/react-devtools#local-development