NodeJS v15.7.0 | Electron v11.2.3 | Uncaught Error: Uncaught TypeError: Class constructor <Class> cannot be invoked without 'new', but is?

Viewed 167

The following webPreferences properties are enabled in the BrowserWindow Object I created:

{
    webPreferences:
            {
                nodeIntegration:false,
                enableRemoteModule:false,
                contextIsolation:true,
                worldSafeExecuteJavaScript:true,
                preload:path.join(__dirname,"preload.js"),
                webSecurity:true,
                webviewTag:false,
                allowRunningInsecureContent:false,
                nodeIntegrationInSubFrames:false,
                nodeIntegrationInWorker:false,
            }
}

My preload.ts:

electron.contextBridge.exposeInMainWorld(
    "someapiname",
    {
        requireClass(): any
        {
            return require("path/to/module").ClassName
        }
    }
)

My HTML file loads a Script called html.js, which contains:

(function()
{
    const CLASSNAME = window["someapiname"].requireClass()
    const newclass = new CLASSNAME("some param",3)
    newclass.aRandomMethod()
})()

But in the Developer Console of Electron I always get:

Uncaught Error: Uncaught TypeError: Class constructor CLASSNAME cannot be invoked without 'new'

I don't get it...It asks for the new but IT is there? Is there something I don't know or some kind of restriction of Electron (and no, I do not want nodeIntegration to be set to true and any other harmful options like enableRemoteModule)

There's no problem when I create the class in the preload script, but then the methods of the Class go missing. I can only access the properties. I also tried it with that IpcMain and IpcRenderer but I got a different Error (something with Parameter).

I just want to create a Class in the renderer and use its methods.

1 Answers
Related