Electron-vue: how to read json file in static oder assets folder

Created on 16 Jan 2018  路  8Comments  路  Source: SimulatedGREG/electron-vue

Hi,

i tried to read an json file with my configurtion, but I can`t get the path to the assets or static folder.
How can I do that?

regards

import path from 'path'
import { remote } from 'electron'

console.log("getAppPath:", remote.app.getAppPath())
const jsonPath = path.join(remote.app.getAppPath(), '/assets/content.json')
console.log(jsonPath);

Most helpful comment

simply require the file

const json = require("../assets/content.json")

All 8 comments

simply require the file

const json = require("../assets/content.json")

ok, but when I want to read a json file that exists direct in a content folder parallel to the app. while runtime.

either you use the method i've described or you can use this

import fs from "fs";

fs.readFile("/path/to/file.json", "utf8",  (err, data)  => {
    if (err) throw err;
    const obj = JSON.parse(data);
});

ok, thats clear, but how can I get the working path of the app in dev and prod mode?

i tried remote.app.getAppPath("exe")) which give the path to the node_moduleselectrondistresourcesdefault_app.asar

remote.app.getPath("exe")) gives the path to electron.exe in
node_moduleselectrondistelectron.exe

what I want to do is to have a conent folder next to the folder with the exe in "win-unpacked". how can I get a base path to get the files in with ../content/content.json" or something

or should I check if getPath("exe") is located in the "electron/dist" folder or my exe is the exe in the "win-unpacked" folder?

The __static variable is created exactly for these use cases. Please refer to the documentation that @erguotou520 posted.

https://www.npmjs.com/package/electron-directory just check the coding of this package.

In my electron app, I am shipping database as well with exe, in dev mode current working directory is showing one and after making exe its going to C drive some Temp folder...so database server couldn't start because these directory issues..How i solved is, i was using postgres database without installing ( https://stackoverflow.com/questions/26441873/starting-postgresql-and-pgadmin-in-windows-without-installation ). That is, when run exe the database server will also start automatically and database server is located in asset folder of my application. I think you got what i said..because of the directory path issue as same like your issue, what i done the pgsql folder separately stored in C drive and run the pgsql server from outside exe..

One another way i found is, if you unpack app.asar you can see subdirectory assets there and your js file as well. how to unpack please refer this ( https://medium.com/how-to-electron/how-to-get-source-code-of-any-electron-application-cbb5c7726c37 ). By default '__dirname' is located to the app.asar/dist only inside you can access 'asset' folder, then .js file...

All suggestions people saying you can check by building .exe file of application each time...good luck...

@SimulatedGREG am I missing some doc somewhere which explains how to make __static work? It is undefined in App.vue, background.js, and everywhere else I try. I cannot find any reference to it in my project folder.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

simdax picture simdax  路  3Comments

Quadriphobs1 picture Quadriphobs1  路  3Comments

kinoli picture kinoli  路  3Comments

Oriol-GG picture Oriol-GG  路  3Comments

blackw212 picture blackw212  路  3Comments