szwacz / fs-jetpack

Better file system API for Node.js
MIT License
777 stars 41 forks source link

electron cannot find the fs-jetpack module in production #98

Open hjiul opened 3 years ago

hjiul commented 3 years ago

Hi everyone,

I am currently making a small software that require a function to backup or load the database as a json file.

The app was made using the vue cli and is build with electron-builder.

To save the file locally, i use a module called fs-jetpack (which i assume uses the fs module itself but im not sure of that).

Everything works fine in development. I can build the app.

But if i run it, i get the following error :

`Uncaught Error: Cannot find module 'fs-jetpack' Require stack:

I configured the node integration to true. I tried requiring the module both with or without remote.

I have no clue what i can do, I can only guess it has to do with the electron-builder configuration ? If anyone can help, that would be awesome.

Also about fs, my understanding is that browsers are not supposed to be able to use it but there are many electron apps that have a file saving system to the local disk so i guess it is possible somehow.

If not with fs, any way i can write and read file on the local disk. My research only pointed me to localstorage api from chromium but its not 100% persistant if i understand correctly so it wont do it.

Let me know for any piece of code that might help figure things out :slight_smile:

Thanks, Esteban

szwacz commented 3 years ago

For some reason you've found correct guy because I'm also familiar with Electron platform ;)

Yes you can use fs (and fs-jetpack is built on top of fs) in the browser context of electron app, and you configured the node integration so it should already be fine.

Where did you put in package.json the fs-jetpack dependency? Electron builder packs to production bundle only dependencies, and filters out all devDependencies, so if the declaration is in the latter, this is most likely the culprit.

hjiul commented 3 years ago

Hi and thanks for your answer,

It was on the regular dependencies yes.

But I actually found a solution around : ` function writeToFileSync2(filepath, content) { if (window && window.require) { const fs = window.require('fs') fs.writeFileSync(filepath, JSON.stringify(content))

}

} `

I switch to the regular fs module though (for my needs its alright i guess) because that is how i found the exemple but i guess you could apply the same logic with jetpack.

Which is a great module btw, Thanks for that