【发布时间】:2021-10-17 08:11:39
【问题描述】:
在标记为重复之前,我已经搜索了很多,但找不到我需要的答案。
我在使用 express 和 electron 时遇到了一些问题。当我运行npm start 时一切正常(package.json 中的start 脚本是electron .)。使用electron-builder 构建电子后出现问题。它为 windows x-64 构建项目并在 dist 文件夹中创建 .exe 文件。在我想运行可执行应用程序之前,一切看起来都很好。出现了这个对话框:
我进行了很多搜索,但找不到问题所在。实际问题是,电子在构建后无法以某种方式需要本地 js 文件。我在构建过程中尝试了 asar true 和 false。
这是我的项目树:
app
├── node_modules
│ └── ...
├── Controllers
│ ├── baseController.js
│ └── homeController.js
├── Database
│ ├── journals.json
│ └── users.json
├── Models
│ ├── Journal.js
│ ├── User.js
│ └── index.js
├── Repository
│ ├── motor
│ | ├── generator.js
│ | └── index.js
│ ├── core
│ | └── index.js
│ ├── init.js
│ └── index.js
├── Views
│ └── index.html
├── www
│ └── assets
│ └── images
│ └── ...
├── app.js
├── package.json
├── package-lock.json
├── startup.js
└── server.js
这是我的app.js:
const path = require('path');
const server = require(path.join(__dirname, 'server'));
const port = 7970;
const electron = require('electron');
const url = require('url');
const { app, BrowserWindow, Menu } = electron;
let index;
// App Ready
app.allowRendererProcessReuse = false;
app.on('ready', function () {
index = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: true,
nodeIntegrationInSubFrames: true,
contextIsolation: false,
enableRemoteModule: true
},
frame: true,
minimizable: false,
maximizable: false,
fullscreen: false,
resizable: false,
movable: true
});
index.loadURL(url.format({
pathname: path.join("localhost:" + port),
protocol: 'http:',
slashes: true
}));
Menu.setApplicationMenu(null);
});
这里是startup.js,错误实际上是从堆栈跟踪开始的:
const path = require('path');
var express = require('express');
var router = express.Router();
var controllers = require(path.join(__dirname, '/Controllers/baseController'));
router.get('/', controllers.home.index)
module.exports = router;
我已经尝试过使用像 require("./whatever") 这样的相对路径的 require
并没有什么不同。
【问题讨论】:
标签: javascript node.js express electron electron-builder