如何将主代码与 index.js electron 分开?

How to keep the main code separately from index.js electron?

是否可以保留此项目结构并使其正常工作?

我想实现的(基本的电子结构,但主要代码保存在另一个文件夹中): Package.json 的“开始”:“...”加载到 index.js

index.js 加载到 MainCode 文件夹中的 index.html

(请忽略index.py)

这是 index.js

的开始代码
const { app, BrowserWindow } = require("electron");
function createWindow() {
    // Create the browser window.
    let win = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            nodeIntegration: true
        }
    });
    // and load the index.html of the app.
    const path = require ("path");
    win.loadFile (path.join (__dirname, "MainCode", "index.html"));
}
app.on("ready", createWindow);

这不是以前的代码,这是其中一个答案建议的更新代码。

这是 package.json

{
  "name": "filler",
  "version": "1.0.0",
  "description": "This is to distract you",
  "main": "index.js",
  "scripts": {
    "start": "electron .",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "electron": "^11.2.2",
    "jquery": "^3.5.1"
  }
}

编辑:我尝试按照其中一条评论的建议进行操作,但出现此错误:

filler@1.0.0 start /home/avinav/Documents/PYTHON/helpingTEACHERS!/Filler electron .

sh: 1: 电子: 未找到 错误!代码生命周期 错误!系统调用产生 错误!文件sh 错误!错误的恩诺特 错误! filler@1.0.0 开始:electron . 错误!生成 ENOENT 错误! 错误!在 filler@1.0.0 启动脚本处失败。 错误!这可能不是 npm 的问题。上面可能有额外的日志输出。

npm 错误!此 运行 的完整日志可在以下位置找到: 错误! /home/avinav/.npm/_logs/2021-02-04T14_07_18_026Z-debug.log 我认为这与package.json有关,但我不知道到底发生了什么

要将 index.html 文件从文件夹 MainCode 加载到您正在实例化的 BrowserWindow 中,您可以通过替换

win.loadFile ("index.html");

win.loadFile ("MainCode/index.html");

但是,使用以下代码更便于移植:

const path = require ("path");
win.loadFile (path.join (__dirname, "MainCode", "index.html"));

这将根据当前目录创建适合您OS的文件系统路径(__dirname存储当前执行脚本所在目录的路径Node.js ).