I have installed Buffer module via npm install Buffer on my machine and I want to simply import it to the renderer process to use the Buffer
When I use this:
const Buffer = require('Buffer')
it says require is undefined.
None of the solutions on Stack Overflow are working.
Advertisement
Answer
Make sure you have nodeIntegration in your BrowserWindow settings set to true and contextIsolation set to false like so:
new BrowserWindow({
webPreferences: {
nodeIntegration: true,
contextIsolation: false
},
});
By default nodeIntegration is false which stops you from using NPM modules in the renderer-process, turning on nodeIntegration will fix this.
NOTE: To access the Node.js API from the Renderer process, you need to set the
nodeIntegrationpreference to true and thecontextIsolationpreference to false.
Disclaimer, turning on nodeIntegration opens up security holes in your app. See Zac’s answer on how to fix them.