【问题标题】:Electron: my custom config.json is not being loadedElectron:我的自定义 config.json 没有被加载
【发布时间】:2017-05-19 19:42:19
【问题描述】:

我是 Electron 框架的新手。我在我的项目根目录中添加我的自定义 config.json 并在文件 ma​​in.js 中访问它。当我使用 npm start 执行项目时,此文件已成功加载,但是当我使用 electron-builder 创建 exe 文件时,我收到一条错误消息:-

Uncaught Error: ENOENT: no such file or directory, open 'C:\Users\Rohaan Ishfaq\AppData\Local\Programs\examsoft_V0.20\config.json

这是 main.js 文件:-

// Module for creating child processes
const spawn = require('child_process').spawn;
// Module for path manipulations
const path = require('path');
// Module to create calls between windows
const ipc = require('electron').ipcRenderer;
// Module jQuery
const $ = require('jquery'); 
// Module for file manipulations
const fs = require('fs');
// config json
const config = JSON.parse(fs.readFileSync('config.json', 'utf-8'));

(function main() {

    init();
    events();

    function init () {
        createSSHKey();
    }

    function events() {

        // get login btn by id
        const loginBtn = $('#login');
        // register event listener
        loginBtn.click(function (event) {
            ipc.send('change-to-login');
        });
    }

    function createSSHKey() {
        if(!fs.existsSync(config.utilsPath))
            alert(config.utilsPath + ' doesn\'t exist');
        else {
            var sshKeyPath = path.normalize(config.sshKeygenPath);
            //trigger .bat file
            ls = spawn('cmd.exe', ['/c', sshKeyPath]);

            //on data
            ls.stdout.on('data', function (data) {
                console.log('stdout: ' + data);
            });

            //on error
            ls.stderr.on('data', function (data) {
                alert('Error Occured: '+ data);
                process.exit(1);
            });

            //on exit
            ls.on('exit', function (code) {
                alert('SSH key pair has been created successfully. Check '+config.usersPath+process.env.USERNAME+config.sshDirectoryPath+' directory');
                enableLogin();
            });
        }
    }

    function enableLogin() {
        $('#login').text('Start');
        $('#login').prop('disabled', false);
    }

})();

有谁遇到过此类问题并成功解决的?

【问题讨论】:

    标签: javascript json node.js electron electron-builder


    【解决方案1】:

    因为您的应用已打包到 asar 存档中。要直接访问您的文件,请配置extraFiles 选项。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2015-02-17
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    相关资源
    最近更新 更多