简体   繁体   中英

Electron with strapi, Cannot find module package.json

I built an application using strapi , i am trying to package it inside electron using electron-builder.

The packaging is done well, but when i start the app, it shows this message

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.

What seems to be the problem is that my application is looking to package.json file inside the root folder (directly relative to the exe file), while it exist inside the (app) folder with all other resources of the system.

this is my package.json file

{
  "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",
    .......
}

And this is my electron (index.js) file

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()
    }
})

I tried multiple solutions but none work, i hope someone can help with it.

Thanks in advance.

It turned out to be easy fix, with dir option of strapi constructor

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM