electron-userland / electron-json-storage

:package: Easily write and read user settings in Electron apps
1.43k stars 79 forks source link

error in production #150

Closed Holybasil closed 3 years ago

Holybasil commented 3 years ago

electron: 8.2.0 electron-json-storage: 4.2.0

webPreferences:
      process.env.NODE_ENV === 'development' || process.env.E2E_BUILD === 'true'
        ? {
            nodeIntegration: true
          }
        : {
            // nodeIntegration: true,
            preload: path.join(__dirname, 'dist/renderer.prod.js')
          }

And, image.png

jviotti commented 3 years ago

Hey @Holybasil,

I believe you need to set nodeIntegration: true in order to access the process global object.

What I think its happening is that you can always access process at this point. If the environment is production, then you don't enable nodeIntegration on that renderer process, which eventually makes rimraf crash, as that module presumable does try to access process.

In summary, this doesn't seem related to electron-json-storage, but to the fact that another module (rimraf) is trying to access process, which you are disabling if the environment is production.

Holybasil commented 3 years ago

I have found a solution.

main.js

import Storage from 'electron-json-storage';
Storage.set("foo", {value: false});
ipcMain.handle(
  'getStoreValue',
  (event, key) =>
    new Promise((resolve, reject) => {
      Storage.get(key, (error, data) => {
        if (error) {
          reject(error);
        } else {
          resolve(data);
        }
      });
    })
);

renderer.js

import { ipcRenderer } from 'electron';
ipcRenderer
    .invoke('getStoreValue', 'foo')
    .then(data => console.log(data, 'data')); // {value: false} "data"

And 'set' is just like this way.