带 strapi 的电子,找不到模块 package.json

Electron with strapi, Cannot find module package.json

我使用 strapi 构建了一个应用程序,我正在尝试使用 electron-builder 将它打包到 electron 中。

打包的很好,但是当我启动应用程序时,它显示了这个信息

PS E:\Development\DentalSystem\dentalBE_test\dist\win-unpacked>
(node:13196) UnhandledPromiseRejectionWarning: Error: Cannot find module 'E:\Development\DentalSystem\dentalBE_test\dist\win-unpacked\package.json'
Require stack:
- E:\Development\DentalSystem\dentalBE_test\dist\win-unpacked\resources\app\node_modules\strapi\lib\Strapi.js
- E:\Development\DentalSystem\dentalBE_test\dist\win-unpacked\resources\app\node_modules\strapi\lib\index.js
- E:\Development\DentalSystem\dentalBE_test\dist\win-unpacked\resources\app\index.js
-
    at Module._resolveFilename (internal/modules/cjs/loader.js:797:17)
    at Function.o._resolveFilename (electron/js2c/browser_init.js:281:679)
    at Module._load (internal/modules/cjs/loader.js:690:27)
    at Function.Module._load (electron/js2c/asar.js:769:28)
    at Module.require (internal/modules/cjs/loader.js:852:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at new Strapi (E:\Development\DentalSystem\dentalBE_test\dist\win-unpacked\resources\app\node_modules\strapi\lib\Strapi.js:94:21)
    at module.exports (E:\Development\DentalSystem\dentalBE_test\dist\win-unpacked\resources\app\node_modules\strapi\lib\Strapi.js:564:18)
    at createWindow (E:\Development\DentalSystem\dentalBE_test\dist\win-unpacked\resources\app\index.js:23:5)
(node:13196) 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:13196) [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.

问题似乎是我的应用程序正在查找根文件夹中的 package.json 文件(直接相对于 exe 文件),而它与所有其他资源都存在于 (app) 文件夹中系统的。

这是我的 package.json 文件

{
  "name": "DentalSys",
  "main": "./index.js",
  "version": "0.2.0",
  "description": "Dental Clinic Management System",
  "productName": "DentalSys",
  "build": {
    "appId": "com.kldoon.dentalSys",
    "asar": false    
  },
  "scripts": {
    "develop": "strapi develop",
    "strapi-start": "strapi start",
    "strapi-prod": "cross-env NODE_ENV=production npm start",
    "strapi-build": "strapi build",
    .....
  },
  "devDependencies": {
    "concurrently": "^5.1.0",
    "electron": "^9.1.2",
    "electron-builder": "^22.4.1",
    ......
  },
  "dependencies": {
    "knex": "0.20.13",
    "moment": "^2.27.0",
    "sqlite3": "^4.1.1",
    "strapi": "3.0.0-beta.17.5",
    .......
}

这是我的电子 (index.js) 文件

const { app, BrowserWindow, Menu, dialog } = require('electron')
const path = require("path");

const strapi = require('strapi');
//const { exec } = require("child_process");

function createWindow() {

    // Create the browser window.
    const win = new BrowserWindow({
        maximizable: true,
        title: "Dental System",
        webPreferences: {
            nodeIntegration: true
        }
    })
    win.maximize();

    strapi().start().then(() => {
        win.loadURL('http://localhost:49862/');
    }).catch((e) => {
        console.log(e);
    });

    win.on('closed', () => {
        app.quit();
    })
}

app.whenReady().then(createWindow)

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit()
    }
})

app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
        createWindow()
    }
})

我尝试了多种解决方案,但 none 有效,我希望有人能提供帮助。

提前致谢。

事实证明很容易修复,使用 strapi 构造函数的 dir 选项

strapi({
dir: __dirname + '/'     ///<==== add this 
}).start().then(() => {
  win.loadURL('http://localhost:1337/');
}).catch((e) => {
  dialog.showMessageBox(win, { message: JSON.stringify(e) });
  console.log(e);
});