schwift
schwift

Reputation: 339

Access local file in javascript

I have a js app (packaged with Electron) in which I wish to load a yaml file. The following works when I have packaged the app since the app.getAppPath() gives me access to the app.asar file, but in development it returns the path \node_modules\electron-prebuilt\dist\resources\default_app.asar.

fs.readFileSync(`${app.getAppPath()}/src/app/data/items.yml`, 'utf8')

Is there any way to get around this? Should my yaml file not be placed in the same directory as the rest of the app?

Upvotes: 0

Views: 1556

Answers (1)

Vadim Macagon
Vadim Macagon

Reputation: 14847

Use the path module together with the __dirname built-in to construct file paths to assets relative to your source files, the relative paths won't change between development and packaged builds. For example, assuming the following directory structure:

src/
  app/
    browser/
      main.js
    data/
      items.yml

You should reference items.yml in main.js like so:

path.join(__dirname, '..', 'data', 'items.yml')

Upvotes: 1

Related Questions