Kasim Ahmic
Kasim Ahmic

Reputation: 312

Can I specify a config file that an executable compiled with electron-builder can access after packaging?

I'm building an Electron app where a client asks a server for information stored in a JSON file on the server. How can I compile the server app (using electron-builder or other) and then include a JSON file that the compiled executable has access to?

I've look through the Electron and electron-builder docs but I was unable to find any relevant information.

In the end, I'd need the JSON file to be located outside of the packaged server app so that it can be freely modified by the person using it.

I appreciate any and all help!

 

EDIT: I have since solved my issue. Please refer to the post below explaining my solution!

Upvotes: 4

Views: 3603

Answers (1)

Kasim Ahmic
Kasim Ahmic

Reputation: 312

After asking on the Electron Slack chatroom, I was informed that I can use the fs module from Node to reference the file's location and use electron-builder's extraResources option to have that file be moved outside the EXE after compilation.

For example, if you wanted to reference config.json, you would reference it like so in your main.js file:

const { readFileSync } = require('fs');

var configFile = JSON.parse(readFileSync('./config.json'));

Then, in your package.json file, you would use extraResources to tell electron-builder what file to pull from where:

"build": {
    "extraResources": [
        {
            "filter": ["./config.json"]
        }
    ]
}

And of course, with filter being an array, you can continue to specify files that you'd like to remain external just by deliminating them with a comma!

I hope this helps whoever else may have been having issues with it!

Upvotes: 4

Related Questions