How to access a local db on windows through electron

Okay, so here’s where I run into issues. I built for windows, both 32 & 64bit. I tested on both through virtualbox, one machine running windows 10 64bit & the other running Windows 7 32bit. Both run into this same exact error:

Uncaught Error: EPERM: operation not permitted, write fs.js:663

I get two of these errors, the other one is showing for sql.js:3

This runs fine on mac though.

Any thoughts on this error?

If you packaged you’re db in app.asar, you can’t write to it (asar files are read only).
If that’s the case I suggest you’ll use asar --unpack (see details here) which will create a folder called app.asar.unpacked that you will need to distribute along side your app.asar.

Okay, so I changed things up so that i’m not packaging my app in app.asar. I’m creating the db.sqlite when the app is opened and then trying to read from it and i’m still running int othe operation not permitted error. Any ideas?

Paste the error you are getting.

Make sure the file exists and that you have permissions to read and write (you can check this using the stats functions from the fs module).

Try to create a file in the same folder as your db and then try to read from it (do this using the fs module). If it doesn’t work, consider creating the db elsewhere.

Uncaught Error: EPERM: operation not permitted, write  fs.js:663

\\VBOXSVR\Windows_Shared\electron-quick-start-win32-ia32\resources\app\node_modules\sql.js\js\sql.js:3 Uncaught Error: EPERM: operation not permitted, write

These are the errors I’m getting. I will try what you suggested about the file now and see what it does.

Try doing it on the local disk and not through a network share.

It works perfectly on my mac machine. I tried running it on a native windows machine running windows 10, and got the same errors. Tried running on the local disk of my virtualbox machine and got the same errors as well.

Did you try getting the stats from fs for the db file?
Did creating a file at the same location and reading it’s content work?

You can also try using node v4 and seeing if it can load the db. If it can’t on Windows I suggest you open an issue in the sql.js repo.

So I tried using fs to create just a “text.txt” file using the following code. I’m still learning, so I may be completely wrong, but I used the following code:

fs.writeFileSync(__dirname + "/tmp/test.txt")

And commented out all the database code. This still gives me the same EPERM: operation not permitted, write error as before. So I’m assuming I’m doing something wrong with writeFileSync?

You should pass it some data as the second parameter (e.g. fs.writeFileSync(__dirname + '/tmp/test.txt', 'Hello World')), but since you still get the EPERM it seems it still tries.

Something is wrong with you’re permissions. On you’re Windows PC (not through VM), try to create a file in that location by right clicking in the folder and select “New”->“Text Document”. If it doesn’t work, you don’t have permissions there. If it does, try to fs.statSync(pathToDbFile) and paste the response.

Some general tips about the location of the file:

Thank YOU, guys for this solutions. My only 2 errors after “switching” to electron.

I was having the same (relative) errors-problem as cenglish on both Windows 32 & 64 platforms, but NO problems on OSX BUT with NW.js. I tried for MONTHS starting about early November 2015 trying to get SQLjs, Alasql and Node sqlite3 to work on NW and various sqlite work arounds.

Yesterday I thought of trying another alternative because I kept on running into road blocks with anything sqlite/3 builds on NW. I installed electron, the sample app and tested the build of node sqlite3. I only had ONE error which I found the solution here: https://github.com/electron/electron-rebuild/issues/39. Node sqlite3 installed, built and tested PERFECTLY on electron on the SAME Node.js configuration.

I installed and tested SQLjs: PERFECT; Installed & tested alasql. alasql(ATTACH SQLITE DATABASE…) didn’t work on NW but works PERFECTLY on #electron (5 minutes ago).

COULDN’T get node-pre-gyp and nw-gyp from stop throwing errors on NW.

I WISH I had found electron earlier. TWO thumbs and TWO big toes UP for electron.

2 Likes

Thanks for this! If I understand you correctly, you are having greater success with SQL.js on electron. I think I have many new gray hairs from trying the node-sqlite3 approach, even on OS X.

Hi!
I am new to electron. I installed the sqlite3 using npm and having the same structure of files as mentioned above except that In my sqlite3 folder I am not having any tmp folder and ofcourse not to mention db.sqlite. Please guys help me. I am using windows 7 64 bit OS. node v6.5.0, npm v3.10.6 and sqlite3 3.1.4. I do not know what I am missing.

Does anybody know how to setup realm in electron n where to write the code(in main or renderer?).