1

In our electron app, there is a need to update a file with user-entered data (Using node fs module to update the file). It works fine in Development environment, but not working in Production Environment. Do we need to take care of any additional settings somewhere in electron app environment to allow write operation on files? (Tried with XLSX and JSON files, both are giving same issues)

Note: File does exist in our project structure itself. Read operation is working fine though (in Production as well).

Edit: App is built using 'electron-builder' npm package. enter image description here

Rama Rao M
  • 2,961
  • 11
  • 44
  • 64
  • 1
    Please [edit] your question and include details on how you build your app. Might be a problem with packaging to ASAR. – Alexander Leithner Mar 14 '22 at 07:52
  • Hi, we used electron-builder npm to build app, and I see the file also gets copied to asar (I checked it by extracting the asar file and XSLX file does exist in the build folder) – Rama Rao M Mar 15 '22 at 08:34

2 Answers2

2

Files should be written to app.getPath("userData") in an application. Stick this in your main.js file and check to see if this path is the one you are writing files to. This should work in any environment.

const { app } = require("electron");

console.log(app.getPath("userData"));
reZach
  • 8,945
  • 12
  • 51
  • 97
  • OK...Btw, as have mentioned in Q, W're not trying to create a new file, the file does already exist in the project folder structure itself. And also I see the file is accessible, as, in the initial load, we read the existing data of the file and show it in the table view, and it's working fine. So I assume, nothing is wrong with the file path. – Rama Rao M Mar 15 '22 at 08:26
  • See the other user's answer to your question. There are restrictions from where the app can load external files when packaged. I had the same problems and my issue was resolved when I refactored my app to save/write data to `app.getPath("userData")` -> https://stackoverflow.com/questions/55603051/eacces-permission-denied-open-file-error-on-osx-electron-app – reZach Mar 16 '22 at 02:51
1

Since you are packaging your app into an ASAR file, you cannot write to these two locations. To prevent the app from tampering with its own source code, the archive is read-only.

Thus, either package your app into directories (which would leave your JavaScript source code readable for everyone) or move the files out of the application package. The most suitable place should be in the user's app data directory (like @reZach showed using app.getPath ("userData");) or right beside the executable.

That way, your files remain read- and writable when shipping the app.

Alexander Leithner
  • 3,169
  • 22
  • 33