3

I have a file that should be changable without needing to rebuild the project. I'm therefore using the extraResources located in:

.
├── extraResources
│   └── flags.json
├── src
├── package.json
└── ...

For the build process my package.json contains the following:

{
    "build": {
        "extraResources": [
            "./extraResources/**"
        ]
    }
}

This part seems to work since in the build output I get the extraResources in dist/win-unpacked/resources/extraResources/flags.json.

The above was done following this: https://stackoverflow.com/a/46033483/2230045

I'm loading this file via:

const filepath = path.join(app.getAppPath(), 'extraResources', 'flags.json');
const data = fs.readFileSync(filepath, 'utf-8');

This works fine during debugging but after the build, it fails with:

Uncaught Exception: Error: ENOENT, extraResources\flags.json not found in C:\dev\MyProgram\dist\win-unpacked\resources\app.asar

It is not clear to me why this is not working while it seemingly works for others.

Based on a hint from here I tried using hazardous but that did not help.

Spenhouet
  • 6,556
  • 12
  • 51
  • 76

1 Answers1

0

This is because the appPath is different:

  • when you start your development server it is e.g. C:\dev\MyProgram\dist
  • when you start the executable in the packaged directory it is e.g.
    C:\dev\MyProgram\packages\win-unpacked\resources\app.asar
    • and the extra resources that you specified are adjacent to app.asar
    • e.g. C:\dev\MyProgram\packages\win-unpacked\resources\extraResources\flags.json

An easy fix is to just delete the asar part from the path:

const appPath = app.getAppPath().replace('app.asar', '');
const filepath = path.join(appPath, 'extraResources', 'flags.json');

This will work in both cases: i.e. when it does not exist in the path the path is not changed

TmTron
  • 17,012
  • 10
  • 94
  • 142