I have a few JSON files inside a directory named data in the working directory of my Electron project. I succeeded in building the app using electron-build
with the following configuration (package.json).
{
"name": "My App",
"version": "0.0.9",
"description": "TEST DESC",
"main": "main.js",
"scripts": {
"start": "electron .",
"pack": "build --dir",
"dist": "build"
}
"author": "Test",
"license": "CC0-1.0",
"build": {
"appId": "test.tester.test",
"directories": {
"app": ""
},
"extraFiles": [
"data"
],
"dmg": {
"contents": [
{
"x": 110,
"y": 150
},
{
"x": 240,
"y": 150,
"type": "link",
"path": "/Applications"
}
]
},
"win": {
"target": "squirrel",
"icon": "build/icon.ico"
}
},
"devDependencies": {
"electron": "~1.7.8",
"electron-builder": "^20.11.1"
},
"dependencies": {
"electron-settings": "^3.1.4",
"jquery": "^3.3.1",
"leveldown": "^3.0.0",
"mkdirp": "^0.5.1",
"shelljs": "^0.8.1"
}
}
I suppose the data directory is not getting added to the build file. Because I am using the files inside of the data directory to render the views in the app, which is not working in the built app. Please suggest a solution.
I am using these JSON files inside the app like this:
fs.readFile('./data/userdata.json', 'utf8', function readFileCallback(err, data) {
if(data == '') { data = '[]'; }
var users = JSON.parse(data);
//Render Code
});
"build": {
"extraResources": [
{
"from": "data",
"to": "data"
}
]
}
After add like this then Electron-builder will copy the data folder to app's resource/data after packing the app. So that you can read the files with this.
const dataPath =
process.env.NODE_ENV === 'development'
? path.join(__dirname, '../../data')
: path.join(process.resourcesPath, 'data');
fs.readFile(path.join(dataPath,'userdata.json', 'utf8', function readFileCallback(err, data) {
if(data == '') { data = '[]'; }
var users = JSON.parse(data);
//Render Code
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With