Electron应用程序在编译并放入网络共享文件夹后无法打开

yr9zkbsy  于 2023-08-01  发布在  Electron
关注(0)|答案(3)|浏览(241)

我做了一个电子应用程序来管理一些简单的NeDB数据库。当从VS Code启动时,一切都完美地工作,没有错误,也没有奇怪的事情发生。问题来后,我运行npm run package,它创建输出文件夹,因为它应该,但如果我移动该文件夹到网络路径,并运行.exe它打开,保持空了几秒钟,然后关闭没有任何错误。
我检查了文件夹权限(即使我创建了它并完全控制了它)。然后,我试着在我的电脑上运行output.exe,它工作得很好,我还把它复制到另一台电脑上,它也在那里工作。
另一个奇怪的事情是,它崩溃后,我试图删除文件夹,我得到一个消息,有些文件不能删除,由于他们正在使用的另一个程序,所以为了删除文件夹,我必须重新启动我的电脑,因为没有什么在任务管理器杀死.
这是我的package.json

{
  "name": "gestione-marcature-laser",
  "version": "2.0.0",
  "description": "Gestore Marcature Laser",
  "main": "index.js",
  "scripts": {
    "start": "electron-forge start",
    "package": "electron-forge package",
    "make": "electron-forge make"
  },
  "author": "Nemanja G.",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "electron-prompt": "^1.7.0",
    "electron-squirrel-startup": "^1.0.0",
    "jquery": "^3.5.1",
    "moment": "^2.29.1",
    "nedb-async": "^0.1.6",
    "select2": "^4.0.13",
    "xlsx": "^0.16.9"
  },
  "devDependencies": {
    "@electron-forge/cli": "^6.2.1",
    "@electron-forge/maker-deb": "^6.2.1",
    "@electron-forge/maker-rpm": "^6.2.1",
    "@electron-forge/maker-squirrel": "^6.2.1",
    "@electron-forge/maker-zip": "^6.2.1",
    "@electron-forge/plugin-auto-unpack-natives": "^6.2.1",
    "electron": "^25.2.0"
  }
}

字符串
这是我的forge.config.js

const path = require('path')

module.exports = {
  packagerConfig: {
    icon: './assets/logo',
    asar: {
      //unpack: "**/node_modules/pdf-to-printer/**/*"
    },
    out: "gestione-marcature"
  },
  rebuildConfig: {},
  makers: [
    {
      name: '@electron-forge/maker-squirrel',
      config: {
        iconUrl: path.resolve(__dirname, './assets/logo.ico'),
        setupIcon: path.resolve(__dirname, './assets/logo.ico'),
      },
    },
    {
      name: '@electron-forge/maker-zip',
      platforms: ['darwin'],
    },
    {
      name: '@electron-forge/maker-deb',
      config: {
        icon: './Styles/static/icon.png',
      },
    },
    {
      name: '@electron-forge/maker-rpm',
      config: {},
    },
  ],
  plugins: [
    {
      name: '@electron-forge/plugin-auto-unpack-natives',
      config: {},
    },
  ]
};


我的main.js

const { app, BrowserWindow, dialog } = require('electron');
const path = require('path');
const url = require('url');
const { ipcMain } = require('electron');
const bcrypt = require('bcryptjs');
const prompt = require('electron-prompt');

/* SEZIONE PASSWORD, attualmente quella di sap. Usare codice sotto per generare hash */
/*
const password = "PASS"
const saltRounds = 10

bcrypt.genSalt(saltRounds, function (err, salt) {
    if (err) {
        throw err
    } else {
        bcrypt.hash(password, salt, function (err, hash) {
            if (err) {
                throw err
            } else {
                console.log(hash);
                dialog.showErrorBox('Errore', hash);
                //$2a$10$FEBywZh8u9M0Cec/0mWep.1kXrwKeiWDba6tdKvDfEBjyePJnDT7K
            }
        })
    }
})
*/
//hash = "$2b$10$6OT.h/W3EnHAxiRC15OfR.BdEseDilja8m5Z2LPtEeed3pNYNsJwO";
hash = "$2b$10$OMZlnwGs8av1Kwhbx5ya5uUU2HMCdU.Gyn//s0.J2/mSklzoBEjny";

let win = null;
let win2 = null;

function createWindow() {
    // Create the browser window.
    win = new BrowserWindow({
        width: 1000,
        height: 720,
        webPreferences: {
            nodeIntegration: true,
            preload: path.join(__dirname, 'preload.js')
        },
        icon: path.join(__dirname, './assets/logo.png'),
        show: false
    });

    // maximize it
    win.maximize();

    // and load the index.html of the app.
    win.loadURL(url.format({
        pathname: path.join(__dirname, 'index.html'),
        protocol: 'file:',
        slashes: true
    }));

    win.once('ready-to-show', () => {
        win.show()
    });

    win.on("close", (evt) => {
        //Cancel the current event to prevent window from closing

        //options object for dialog.showMessageBox(...)
        let options = {
            type: "question",
            buttons: ["&Sì", "&No", "&Cancella"],
            defaultId: 2,
            title: "Attenzione!",
            message: "Confermi la chiusura del programma Gestione Database Marcature?",
            detail: "Potrai riaprirlo dall'icona nel desktop.",
            icon: path.join(__dirname, './assets/logo.png'),
            cancelId: 2,
            noLink: true,
            normalizeAccessKeys: true
        };

        let res = dialog.showMessageBoxSync(win, options);
        if (res === 0) {
            //Yes button pressed
        } else if (res === 1) {
            //No button pressed
            evt.preventDefault();
        } else if (res === 2) {
            //Cancel button pressed
            evt.preventDefault();
        }

    });

    // Open the DevTools.
    win.removeMenu();
    //win.webContents.openDevTools();
}

ipcMain.on('openPage', async (_, arg) => {
    if (!win2) {
        let pwd = await prompt({
            title: 'Attenzione!',
            label: 'Inserire la password:',
            type: 'input',
            height: 200,
            inputAttrs: { type: 'password', required: true },
            icon: path.join(__dirname, './assets/logo.png'),
            menuBarVisible: false,
            buttonLabels: 'ok',
            customStylesheet: path.join(__dirname, 'styles', 'prompt-styles.css')
        });

        bcrypt.compare(pwd, hash).then(res => {
            if (res) {
                win2 = new BrowserWindow({
                    parent: win,
                    webPreferences: {
                        nodeIntegration: true,
                        preload: path.join(__dirname, 'preload.js')
                    },
                    frame: false,
                    width: 1000,
                    height: 720,
                    show: false
                });

                win2.maximize();

                win2.loadURL(url.format({
                    pathname: path.join(__dirname, arg.page),
                    protocol: 'file:',
                    slashes: true
                }));

                win2.once('ready-to-show', () => {
                    win2.show()
                });

                win2.on('close', _ => { win2 = null });

                //win2.webContents.openDevTools();
            } else {
                dialog.showErrorBox('Errore', 'Password non corretta!');
            }
        });
    }
});

ipcMain.on('closePage', _ => {
    win2.close();
});

ipcMain.on('showMessageBox', (_, arg) => {
    dialog.showMessageBox({
        title: arg.title,
        message: arg.message
    })
});

const gotTheLock = app.requestSingleInstanceLock();

if (!gotTheLock) {
    app.quit();
} else {
    app.on('second-instance', (event, commandLine, workingDirectory) => {
        // Someone tried to run a second instance, we should focus our window.
        if (win) {
            if (win.isMinimized()) {
                win.restore();
            }
            win.focus();
        }
    })

    // This method will be called when Electron has finished
    // initialization and is ready to create browser windows.
    // Some APIs can only be used after this event occurs.
    app.whenReady().then(createWindow);

    // Quit when all windows are closed.
    app.on('window-all-closed', () => {
        // On macOS it is common for applications and their menu bar
        // to stay active until the user quits explicitly with Cmd + Q
        if (process.platform !== 'darwin') {
            app.quit();
        }
    });
}

* 编辑 *

我尝试了几件事:
1.我使用电子锻造网站(https://www.electronforge.io/)上的npm init electron-app@latest my-app创建了另一个应用程序。
1.然后我运行npm run make,把.exe放在网络路径中,仍然是同样的问题。
1.为了确保这不是一个问题,我们的服务器,我共享文件夹到另一台计算机,并试图运行.exe和.同一问题
似乎有一些错误试图运行电子可执行文件从一个共享文件夹,但似乎不能找出原因

* 编辑2 *

尝试从终端启动应用程序,这是输出:

[9836:0726/160545.734:ERROR:gpu_process_host.cc(948)] GPU process launch failed: error_code=18
[9836:0726/160545.780:ERROR:gpu_process_host.cc(948)] GPU process launch failed: error_code=18
[9836:0726/160545.788:ERROR:gpu_process_host.cc(948)] GPU process launch failed: error_code=18
[9836:0726/160545.794:ERROR:gpu_process_host.cc(948)] GPU process launch failed: error_code=18
[9836:0726/160545.799:ERROR:gpu_process_host.cc(948)] GPU process launch failed: error_code=18
[9836:0726/160545.809:ERROR:gpu_process_host.cc(948)] GPU process launch failed: error_code=18
[9836:0726/160545.814:ERROR:gpu_process_host.cc(948)] GPU process launch failed: error_code=18
[9836:0726/160545.819:ERROR:gpu_process_host.cc(948)] GPU process launch failed: error_code=18
[9836:0726/160545.825:ERROR:gpu_process_host.cc(948)] GPU process launch failed: error_code=18
[9836:0726/160545.825:FATAL:gpu_data_manager_impl_private.cc(431)] GPU process isn't usable. Goodbye.

gg0vcinb

gg0vcinb1#

Electron.js在打包时生成的包含所有应用程序文件和资源的文件夹不用于移动和使用。它被electron-forge打包器使用。
无论如何,您可以使用electron-builder将应用打包为可移植的.exe,其中-p标志用于可移植文件,--win标志用于Windows可执行文件:

electron-builder -p --win

字符串
要使用此模块,您需要设置some things,例如发布提供程序和更新详细信息。
--win flag还有两个替代选项:

  • --mac for MacOS
  • --linux for Linux
p4rjhz4m

p4rjhz4m2#

而不是electron-forge package用途:

electron-forge make

字符串
为什么?

  • 支持package命令不支持的forge.config.js文件
  • 与分布更兼容(在您的情况下,移动到网络路径)
  • 具有比package更多的CLI标志(选项
nnsrf1az

nnsrf1az3#

如果您的应用程序没有将任何错误记录到终端/控制台,则可能意味着gotTheLock变量在共享文件夹上是错误的。由于this issue,Electon无法正确退出。在调用app.quit()之前,您需要删除所有BrowserWindow示例上的事件侦听器(如close),以使其正确退出。

相关问题