TypeError: Cannot read properties of undefined (reading 'members')
FedeIlLeone opened this issue ยท 9 comments
After upgrade to remote v2.0.1 (electron v14.0.1) I can't use the remote module in the renderer process.
The line and where the error comes from the renderer file:
const { dialog } = require("@electron/remote");
Main process:
const { app, BrowserWindow } = require("electron");
const remoteMain = require("@electron/remote/main");
const win = new BrowserWindow({ ... });
remoteMain.enable(win.webContents);
win.loadFile("./build/web/index.html");
Everything was working fine before the upgrade.
Upgraded to v14.0.0, added the @electron/remote npm to dependencies:
"devDependencies": {
"electron": "14.0.0",
"@electron/remote": "2.0.1"
}
The previous working code no longer works:
app.getPath("documents")
ERROR:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'members')
I've tried both ways:
let { app } = require('@electron/remote');
const userDataPath = app.getPath('documents');
and:
let { app } = require('@electron/remote/main');
Hello guys!
Maybe I've found the solution! I was facing the same issue and I've tried a thing...
On the migration doc (https://github.com/electron/remote/blob/main/docs/migration-2.md) none mention the required line of the previous major version:
require('@electron/remote/main').initialize()
I've deleted that on the main process cause I was thinking that was not required anymore.
Well, I was wrong. I've restore that line and everything is working fine now.
I use the code from @FedeIlLeone for give you an example.
Main process:
const { app, BrowserWindow } = require("electron");
const remoteMain = require("@electron/remote/main");
/* add this before the enable function */
remoteMain.initialize();
const win = new BrowserWindow({ ... });
remoteMain.enable(win.webContents);
win.loadFile("./build/web/index.html");
I hope that can help you all!
Valentina
Thank you! That fixed the issue.
On the migration doc (https://github.com/electron/remote/blob/main/docs/migration-2.md) none mention the required line of the previous major version
I think it should be written that you have to use .initialize()
in v2 too.
That worked! Thank you ๐
Doc should also mention needing the remote npm dependency:
"devDependencies": {
"electron": "14.0.0",
"@electron/remote": "2.0.1"
}
Documentation is still not clear enough that you need both the initialize and the enable. I had to search the closed issues on github to realize my mistake.
@itsUndefined both are required with electron v16 and remote module @2.x as mentioned in the doc https://github.com/electron/remote/blob/main/docs/migration-2.md
Thanks, @Valentina16 it works perfectly
For me, after carefully doing the above, remote
was still undefined in the render process. My error was dumb:
import remote from "@electron/remote";
Instead, I needed
import * as remote from "@electron/remote";
or
const remote require("@electron/remote")
Normally the typescript type system would squawk at a problem like this, so maybe this is just something weird in my setup.