Utilizing Electron to save editable user data in a .txt file

I am making an electron app that converts data from .txt files to Javascript arrays. This data is stored inside a folder called faces in the main directory. I also have a button in my app which, when clicked opens file explorer at the faces folder so the user can edit the .txt files. This works fine when running npm start, but then when I use electron builder to package my app, the app can no longer find the .txt files and the user cannot edit them (giving me lots of errors). Is there some way to have a folder of .txt files that the app uses to draw information from with Electron builder?

Edit

Below is the JS used:

//Import Lists from .txt files

var ears = fs.readFileSync('faces/ears.txt', 'utf8').split('\n');
var mouths = fs.readFileSync('faces/mouths.txt', 'utf8').split('\n');
var eyes = fs.readFileSync('faces/eyes.txt', 'utf8').split('\n');

//Opens faces txt docs in file explorer

function edit() {
    shell.openItem(require('electron').remote.app.getAppPath() + '/faces')
}

Here is what happens when I open the packaged app (this is the win-unpacked result but the error is the same for .exe which runs with the installer):

As you can see it does not load an information and you can see it cannot find the faces folder or the .txt files.

Answer №1

The issue seems to be caused by the use of different path-joining characters in various operating systems.

You will notice that before your suffix, /faces, every slash was a backslash (\).

To resolve this problem, consider utilizing the path module.

const path = require(path)

....

function edit() {
  shell.openItem(
    path.resolve(require('electron').remote.app.getAppPath(), 'faces')
  )
}

Answer №2

The constructed application is attempting to write data within its own folder. While this may work during development, it is not possible in a production environment due to the app being packaged as an asar archive (app.asar). The error message indicates that the app is trying to write inside app.asas, which is not a valid directory for writing operations.

To resolve this issue, consider storing this type of information in directories related to user configurations and preferences, rather than where the application is installed.

You can refer to https://www.electronjs.org/docs/api/app#appgetpathname for guidance on retrieving the correct path.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Monitor for the specific parameter in the incoming GET request

My application is using the POST method to submit jobs remotely. After submitting a job, I receive a unique job ID from the POST request that allows me to check the status of the job using a GET request. $http.get('http://localhost:8090/jobs/'+i ...

What is the method in JavaScript for a child function to trigger a Return statement in its parent function?

I have encountered a unique problem. I need to retrieve some data downloaded via ajax and return it, but neither async nor sync modes are fetching the data in time for the return. Is there a way to call the return from a child function to the parent func ...

The response from unirest in node.js came back as undefined

Currently, I am diving into the world of Facebook bots, despite not being well-versed in node.js development. Stepping out of my comfort zone and embracing this new challenge for the first time has been quite exhilarating. Below is the function that I hav ...

Tips for improving the loading speed of a table during scrolling with JavaScript

Is there a way to speed up the loading time of a <table> with 20000 rows? As I scroll through the page, it feels very sluggish and takes around 4-5 seconds to load the remaining table data. I'm unsure how to tackle this issue, which is why I h ...

What is the process for retrieving an Object from $cookies?

I've encountered an issue where I'm storing a user object on a Cookie and, upon the client's return visit to the site, I want to utilize this object's properties. However, upon the client's return, my cookie object appears as [obj ...

TypeScript enabled npm package

I am currently developing a npm module using TypeScript. Within my library, I have the following directory structure: . ├── README.md ├── dist │ ├── index.d.ts │ └── index.js ├── lib │ └── index.ts ├── ...

Vue Method always executed (regardless of click event)

I'm fairly new to vue and still getting a grasp on the fundamentals of my code. Currently, I am facing an issue with a Method. It should only trigger when the user clicks on the button, but it seems to be triggered all the time. I even tried adding ...

serving JSON and HTML responses using a PHP script

After searching through the questions here, I couldn't find a solution. As someone new to PHP and jQuery, I appreciate your patience as I explain my issue. My goal is to send an ajax request using jQuery to a script that runs a mysql query on data fr ...

What method can be used to keep Chromium open while using Puppeteer?

I am trying to figure out how to launch only one instance of Chromium from my first script and then connect to it from subsequent scripts. I am aware of the puppeteer.connect() method, but the issue is that when I start the script responsible for launching ...

Encountering the following error message: "Received error: `../node_modules/electron/index.js:1:0 Module not found: Can't resolve 'fs'` while integrating next.js with electron template."

I am utilizing the electron template with next.js, and I am trying to import ipcRenderer in my pages/index.tsx file. Below is the crucial code snippet: ... import { ipcRenderer } from 'electron'; function Home() { useEffect(() => { ip ...

Keep rolling the dice until you hit the target number

Currently, I am in the process of learning JavaScript and one of my projects involves creating a webpage that features two dice images, an input box, and a button. The objective is for users to input a value, click the button, and then see how many rolls i ...

Limiting access in _app.js using Firebase and Redux

In my application, users can access the website without logging in. However, they should only be able to access "/app" and "/app/*" if they are authenticated. The code I have written seems to work, but there is a brief moment where the content of "/app" ...

What is the best way to traverse through a nested JSON file with d3.js?

Greetings. I am currently facing a challenge in navigating through a nested JSON file. Below is the JSON data that I need assistance with: {"Id":466,"Name":"korea", "Occurrences": ...

CSS - What's the source of this mysterious whitespace?

I'm currently making adjustments to a Wordpress website that is designed to be responsive. The site uses media queries to adapt its display based on the user's screen size, and everything looks great except for when the screen width is at its sma ...

Encountering a "args" property undefined error when compiling a .ts file in Visual Studio Code IDE

I've created a tsconfig.json file with the following content: { "compilerOptions": { "target": "es5" } } In my HelloWorld.ts file, I have the following code: function SayHello() { let x = "Hello World!"; alert(x); } However ...

Methods for creating overlapping background images in breadcrumbs

I am having trouble with making the breadcrumb frame overlap the inside images. I attempted using z-index: -1, but it causes the images to disappear. Please refer to the attached image for a better understanding. Thank you. .breadcrumb { margin-botto ...

Can an unresolved promise lead to memory leaks?

I have a Promise. Initially, I created it to potentially cancel an AJAX request. However, as it turns out, the cancellation was not needed and the AJAX request completed successfully without resolving the Promise. A simplified code snippet: var defer = $ ...

Trouble with minification in Sencha cmd 5

I've been attempting to compress a Sencha 5 project using Sencha CMD, but I keep experiencing failures. sencha generate app -ext demoApp ./demoApp Then, in an effort to compress the application, I entered the following command: sencha app build ...

How do I resolve the issue of a non-iterable 'int' object?

1 How can I troubleshoot the error that is popping up? I believe the issue may be related to it being a dictionary. Any suggestions on how to fix this problem? views search(request): if "q" in request.GET: querystring = request.GET.get(" ...

What causes block elements to act like inline elements?

:target { border: 2px solid #D4D4D4; background-color: #e5eecc; } .navigation li { list-style-type: none; float: left; padding: 1em; } <ul class="navigation"> <li> <a href="#help"> Help </a> </li> <li> & ...