sindresorhus / electron-store

Simple data persistence for your Electron app or module - Save and load user preferences, app state, cache, etc
MIT License
4.61k stars 150 forks source link

Webpack 5 polyfills #199

Closed steveoh closed 3 years ago

steveoh commented 3 years ago

Because of webpack 5 removing polyfills this library no longer works with the electron forge starter project.

I have been able to polyfill everything except for fs via the resolve.fallback webpack config. fs is imported from conf, electron, and path-exists.

resolve: {
  fallback: {
    assert: require.resolve('assert/'),
    crypto: require.resolve('crypto-browserify'),
    os: require.resolve('os-browserify/browser'),
    path: require.resolve('path-browserify'),
    stream: require.resolve('stream-browserify'),
    util: require.resolve('util'),
  },
},
Electron Forge was terminated:
ModuleNotFoundError: Module not found: Error: Can't resolve 'fs' in '.../node_modules/atomically/dist/utils'
Did you mean './fs'?

I've tried the resolve.preferRelative: true, but it gives the same issue without the did you mean text.

Module not found: Error: Can't resolve 'fs' in '.../node_modules/conf/dist/source'

I'm not quite sure how to polyfill fs and if I should have to. What thoughts do folks have on this?

This stack overflow question has some answers about using window.require but I'm not sure how that can be possible with this projects dependencies.

Thanks in advance!

refs #191

steveoh commented 3 years ago

inside my main.js it appear there is access to fs. Learning more about electron might be my issue.

const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const Store = require('electron-store');

const store = new Store();

ipcMain.handle('loadContent', (e) => {
  return store.get('config.key');
});

ipcMain.on('saveContent', (e, content) => {
  store.set('config.key', content);
});

This works.

mikeliao97 commented 3 years ago

@steveoh were you able to install it? curious, same problem. :)

steveoh commented 3 years ago

Yes, our issue was with the misunderstanding of where the node code was allowed to run. Using the ipc bridge to a main process allows this to work properly.

sahil-liscio commented 2 years ago

I have the same issue and I'm not sure how @steveoh solved it. Do i need to have node integration enabled? I still get the error that I need the fs module, if I set fs; false in the fallback then I get another error fs.existsFileSync doesnt exist.

pjoulot commented 1 year ago

I also tried to use a boiler template: https://github.com/electron-react-boilerplate/electron-react-boilerplate And impossible to make this library work with it :(

So I gave up and went for a custom implementation, but would love to replace my custom code with this package if someone finds a solution.

sintanial commented 11 months ago

See https://github.com/sindresorhus/electron-store/issues/259#issuecomment-1817571011