1

当我在 Windows 10 上构建npm start应用程序时,它无法正常运行,而在 macOS 上运行良好。

包.json

{
  "name": "SimpleTimer",
  "version": "1.0.0",
  "productName": "SimpleTimer",
  "copyright": "",
  "description": "",
  "main": "main.js",
  "scripts": {
    "start": "node_modules/.bin/electron .",
    "test": "echo \"Error: no test specified\" && exit 1",
  },
  "keywords": [],
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "electron": "^10.1.2"
  }
}

我收到以下错误消息。

在此处输入图像描述

TypeError: ipcMain.handle is not a function这适用于:

main.js

ipcMain.handle("ipc-timer-start", () => {
  if ( isWorking === true ) {
    return true
  }
  else {
    StartTimer();
    isWorking = true;
  }
  return true;
});

这个函数是从ipcRenderer.invoke()写入的 ipc 通信的接收者preload.js。这就是下面源码invoke()中api中的方法。TimerStart

preload.js

const { contextBridge, ipcRenderer} = require("electron");

contextBridge.exposeInMainWorld(
  "api", {
    TimerStart: () =>
        ipcRenderer.invoke("ipc-timer-start")  /*** HERE ***/
            .then(result => result)
            .catch(err => console.log(err)),

    TimerStop: () => ipcRenderer.send("ipc-timer-stop"),

    TimerReset: () => ipcRenderer.send("ipc-timer-reset"),

    DisplayTimer: (channel, listener) => {
      ipcRenderer.on("ipc-display-timer", (event, arg) => listener(arg));
    }
  }
);

当然,preload.js是在创建时指定的BrowserWindow

main.js

 mainWindow = new BrowserWindow({
    title: config.name,
    width: 1024,
    height: 640,
    minWidth: 1024,
    minHeight: 640,
    webPreferences: {
      worldSafeExecuteJavaScript: true,
      nodeIntegration: false,
      contextIsolation: true,
      preload: __dirname + '/preload.js'  /*** HERE ***/
    }
  });

跳过此错误消息对话框后,我使用 DevTools 进行了检查,看起来好像preload.js没有从Unable to load preload script:.

在此处输入图像描述

鉴于这些陈述,似乎preload.js没有正确包含在构建中,我该怎么办?

再一次,它在 macOS 上运行良好,但由于这些问题,它在 Windows 10 上无法正常运行。

4

1 回答 1

0

我能够自己解决这个问题。

这个问题不在电子中,而是在我的Parallels Desktop for Mac环境中。

我试图通过 Parallels 网络驱动器让 Electron 工作目录在 Windows 10 上运行,但它似乎无法正确识别构建文件路径。

目前,即使您在本地安装了 Electron 而不是全局安装,它似乎也无法正确构建。

npm install electron@latest --save-dev

似乎可以在 macOS 上构建的项目无法直接在 Windows 10 上构建。

Electron 是electron .在 Windows 10 上构建的“”命令,它似乎提取了C:\Users\[UserName]\AppData\Roaming\npm\路径上的必要文件。我不知道确切的原因,但它似乎与本地安装的 Electron 不一致。

如果您想在 Windows 10 上运行您的项目,请复制项目目录并输入“ npm install”命令重新安装 Electron 等 Node 模块。它将自动重新安装中列出的依赖包package.json

结果,我终于node_modules/.bin/electron .可以毫无问题地构建“”来构建它。

于 2020-09-18T07:58:56.590 回答