In Electron, how do I enable NodeJS inside my React Code?
What are the steps I need to take? I'm trying to access the file system component which is normally accessed in node using:
const fs = require('fs');
Inside my React code this didn't compile, so I tried changing it to import and it also did not work.
import fs from 'fs';
// Gives error
Module not found: Error: Can't resolve 'fs' ...
Searching for a solution I found some questions but not an answer (yet):
I saw a SO question/answer that matched this but now I can't find it!
I've already enabled NodeJS in my Electron main app using:
mainWindow = new BrowserWindow({
width: winState.width,
height: winState.height,
x: winState.x,
y: winState.y,
webPreferences: {
// --- !! ANT !! ---
// Disable 'contextIsolation' to allow 'nodeIntegration'
// 'contextIsolation' defaults to "true" as from Electron v12
contextIsolation: false,
nodeIntegration: true, /* Allow NodeJS code to be called in Web Pages */
enableRemoteModule: true
}
})