带trapi的Electron,找不到模块package.json [英] Electron with strapi, Cannot find module package.json

查看:351
本文介绍了带trapi的Electron,找不到模块package.json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 strapi 构建了一个应用程序,我正尝试使用电子生成器将其包装在电子内部。

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.

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

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.

这是我的package.json文件

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

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

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.

预先感谢。

推荐答案

事实证明很容易解决,使用bandi构造函数的dir选项

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

这篇关于带trapi的Electron,找不到模块package.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆