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.