简体   繁体   English

电子:我的自定义config.json未加载

[英]Electron: my custom config.json is not being loaded

I'm new to Electron Framework. 我是Electron Framework的新手。 I'm adding my custom config.json in my project root and accessing it in a file main.js . 我将自定义config.json添加到项目根目录中,并在文件main.js中对其进行访问。 This file is successfully be loaded when I execute project using npm start but when I create exe file using electron-builder , I get an error message:- 当我使用npm start执行项目时,此文件已成功加载,但是当我使用电子生成器创建exe文件时,出现错误消息:-

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

Here is main.js file:- 这是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);
    }

})();

Any one who has faced this kind of issue and fixed it successfully? 有人遇到过这种问题并成功解决了吗?

Because your app is packed into asar archive. 因为您的应用已打包到asar存档中。 To access your files directly, please configure extraFiles option. 要直接访问您的文件,请配置extraFiles选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM