Please describe your issue:
When reading a file from the base directory of your app, you can usually just do fs.readFileSync("data.json") or similar. After packaging the path changes to fs.readFileSync("resources\app\data.json") and previously working code will break.
What command line arguments are you passing? Alternatively, if you are using the API, what
parameters are you passing to the packager() function?
electron-packager .
Please provide either a failing minimal testcase (with a link to the code) or detailed steps to
reproduce your problem. Using electron-quick-start
is a good starting point.
This can easily be tested by installing quick start and changing renderer.cs to somthing like this:
var a = require("fs");
var b = a.readFileSync("data.json", "utf8");
document.writeln(b);
Then package and the script won't work anymore. There are obviously easy workarounds for this, like just changing the path before packaging, but that can become a time consuming and repetitive task. I also spent a couple hours to find this out in the first place.
Whenever you try to load a path in node.js through the fs API's, if you provide a non-absolute path it is resolved relative the the CWD of your application. In dev this is the root of your app, in pacakged this is wherever your exe or .app file is.
You need to resolve absolute paths yourself using path.resolve(__dirname, 'data.json')
@MarshallOfSound Setting Absolute path is not working for me, I have a db file in a directory and I added it to my build file as:
"extraResources": [
"app/clipboarddb/copydata.db",
]
When I look at the packaged apps resources I see the file, but in the Handler I have:
const dbPath = path.resolve(__dirname, 'copydata.db')
The DB file is in the same directory as the Handler
And I still get Error:
Cannot open Database because the directory does not exist
If you put copydata.db into extraResource, it's in the resources directory, not the app directory, per the docs. The resources directory is usually one folder above the packaged base app directory, if I remember correctly.
Most helpful comment
Whenever you try to load a path in node.js through the
fsAPI's, if you provide a non-absolute path it is resolved relative the the CWD of your application. In dev this is the root of your app, in pacakged this is wherever yourexeor.appfile is.You need to resolve absolute paths yourself using
path.resolve(__dirname, 'data.json')