Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hjiul opened this issue Nov 25, 2020 · 2 comments
Open

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

hjiul opened this issue Nov 25, 2020 · 2 comments

Comments

@hjiul
Copy link

hjiul commented Nov 25, 2020

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:

  • electron/js2c/renderer_init
    at Module._resolveFilename (internal/modules/cjs/loader.js:797)
    at Function.i._resolveFilename (electron/js2c/renderer_init.js:43)
    at Module._load (internal/modules/cjs/loader.js:690)
    at Function.Module._load (electron/js2c/asar.js:769)
    at Module.require (internal/modules/cjs/loader.js:852)
    at require (internal/modules/cjs/helpers.js:74)
    at Module.56d7 (app.4238a71e.js:1)
    at o (app.4238a71e.js:1)
    at Object.0 (app.4238a71e.js:1)
    at o (app.4238a71e.js:1) `

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
Copy link
Owner

szwacz commented Nov 26, 2020

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
Copy link
Author

hjiul commented Nov 27, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants