【问题标题】:Express Electron Cannot Require Local Js FilesExpress Electron 不能需要本地 Js 文件
【发布时间】: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


    【解决方案1】:

    Electron 可能没有随安装包一起提供您的文件。尝试将 extraFiles 字段添加到您的 package.json

    所以,在您的 package.json 和您的 build 字段中,添加以下内容:

    build:{
        "appId":"your.product.id",
         ...
        "extraFiles":[
            {
                "from":"Controllers",
                "to":"resources/app/Controllers",
                "filter":["**/*"]
            },
            {
                "from":"Database",
                "to":"resources/app/Database",
                "filter":["**/*"]
            },
            {
                "from":"Models",
                "to":"resources/app/Models",
                "filter":["**/*"]
            },
            {
                "from":"Repository",
                "to":"resources/app/Repository",
                "filter":["**/*"]
            },
            {
                "from":"Views",
                "to":"resources/app/Views",
                "filter":["**/*"]
            },
            {/** You can add more if you need*/}
        ]
    }
    

    默认情况下,Electron 在构建时会忽略所有 devDependencies 和“新”文件。 extraFiles 字段可防止 electron-builder 忽略应用程序执行所需的文件。它实际上将位于from 字段中的文件发送到to 字段中的路径中。 filter 用于在必要时使用扩展名或内部目录保留一些文件。

    extraFiles 的默认路径是您应用的根目录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      • 2013-03-03
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      相关资源
      最近更新 更多