Skip to content
Advertisement

using npm module in renderer process of electron

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.

Read more here

NOTE: To access the Node.js API from the Renderer process, you need to set the nodeIntegration preference to true and the contextIsolation preference to false.

Disclaimer, turning on nodeIntegration opens up security holes in your app. See Zac’s answer on how to fix them.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement