I’m developing a desktop application with Electron, and I have a problem with local data storage. My goal is to check if the user is already logged in using a locally stored token. If the token exists, the application should directly open the main window. Otherwise, it should first display a loading window and a login window (console.log("Token is Null")
).
However, when I try to import the electron-store
library, I get an error.
const Store = require('electron-store');
const store = new Store();
let mainWindow;
function createMainWindow(alrealdyLogin) {
mainWindow = new BrowserWindow({
width: 300,
height: 400,
frame: false,
show: true,
transparent: false,
resizable: false,
fullscreenable: false,
titleBarStyle: 'customButtonsOnHover',
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
enableRemoteModule: false,
nodeIntegration: false
},
icon: path.join(__dirname, 'icon.png')
});
mainWindow.loadFile('html/loading.html');
}
app.on('ready', () => {
const token = store.get('token');
if (token) {
createLoginWindow(false);
createMainWindow();
} else {
createLoadingWindow(true);
createLoginWindow();
}
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
const token = localStorage.getItem('token');
if (BrowserWindow.getAllWindows().length === 0) token == null ? console.log("Token is Null") : createMainWindow();
});
The error:
App threw an error during load Error [ERR_REQUIRE_ESM]: require() of ES Module C:UsersNarzayProjetsMeagan ClientLauncher Electronnode_moduleselectron-storeindex.js from C:UsersNarzayProjetsMeagan ClientLauncher Electronmain.js not supported.
Instead change the require of index.js in C:UsersNarzayProjetsMeagan ClientLauncher Electronmain.js to a dynamic import() which is available in all CommonJS modules.
at c._load (node:electron/js2c/node_init:2:16955)
at Object. (C:UsersNarzayProjetsMeagan ClientLauncher Electronmain.js:3:15)
Alert Pop-Up:
A JavaScript error occurred in the main process
Uncaught Exception:
Error [ERR_REQUIRE_ESM]: require() of ES Module C:UsersNarzayProjetsMeagan ClientLauncher Electronnode_moduleselectron-storeindex.js from
C:UsersNarzayProjetsMeagan ClientLauncher Electronmain.js not supported. Instead change the require of index.js in C:UsersNarzayProjetsMeagan ClientLauncher Electronmain.js to a dynamic import() which is available in all CommonJS modules.
at c._load (node:electron/js2c/node_init:2:16955)
at Object. (C:UsersNarzayProjetsMeagan ClientLauncher
Electronmain.js:3:15)
I would like to recover the “token” value in the “localStorage”:
localStorage.setItem('token', data.token);