7

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
});
tpikachu
  • 4,478
  • 2
  • 17
  • 42
Abhijith C S
  • 89
  • 1
  • 9
  • Just BTW you can see if it's getting included at all by looking into the install directory: `/Users/username/AppData/Local/Programs/appname/resources/app`. That's where it is on windows although I'm not sure where it is for mac or linux – Joshua May 06 '18 at 14:11
  • Are those files read only or do you edit those json files (if read only you can leave them in the asar)? Your extraFiles attribute looks a little bit odd. Your array should contain objects of this type: `{ from: 'sourcePattern', to: 'targetPattern'}` – Rhayene May 07 '18 at 09:26
  • try the app.getAppPath() and app.getPath method. https://electronjs.org/docs/api/app#appgetapppath – linxie May 08 '18 at 15:11

1 Answers1

10
"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
});
tpikachu
  • 4,478
  • 2
  • 17
  • 42
  • is there any way to place the folder in the main directory (i.e. `project/data`) as opposed to placing it in the resources directory (i.e. `project/resources/data`)? – oldboy Feb 10 '20 at 01:14
  • actually i figured it out, i can just change it to `"to": "../projects"`. do you know if `..` also traverses upwards on Linux and MacOS?? – oldboy Feb 10 '20 at 01:21