-
Notifications
You must be signed in to change notification settings - Fork 15.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to use 'remote' module #3755
Comments
I suspect the first line should be: |
Doing so throws this error:
which makes sense, since 'remote' is supposed to be a module within Electron, as far as I understand.
I'm just following this example: https://github.com/atom/electron/blob/master/docs/api/remote.md Am I missing something? Thanks! |
I guess they've changed the way to require Are you using v0.35.0 or above of Electron? |
Are you sure that you are using from the renderer process? |
@lee-dohm I'm running v0.33.8:
Here's my package.json relevant section:
Not sure how to upgrade (if it applies). @herrmannplatz Oops... nope, I was doing so in the main process... Thanks again |
Damn! sorry, please forget it, I now see remote.getGlobal() IS working. |
Hi @protozoo , I'm getting your same error (i.e. As you did I'm using the method found in the API docs: I see that in So how did you solve the issue? Thanks in advance for any input! |
You should not use electron from npm. Also, in case you're using webpack, you'd probably get another errors about |
Aha, I think that Webpack was "the issue" then. Also, can you elaborate why I should not use electron from npm?
|
@miccferr I was talking about the package, electron in npm is not related to Atom Electron, you need to use one of these ways:
Your |
I'm using electron-prebuilt version 0.36.8 and I got this error message: const electron = require('electron');
const remote = electron.remote;
const BrowserWindow = remote.BrowserWindow; Inspection on
I tried the methods above and none worked for me. Any help is appreciated!! |
@tinirlove The |
@tinirlove Did you find a solution for your problem? I got the same error. I get this error:
What follows is a list of mime types... |
@rmnblm No, I did not. I think enlight's answer meant to create a |
@tinirlove Could you provide an example for your solution? 😁 BTW: Do you use Electron with Webpack and React? You can read the full issue here: chentsulin/webpack-target-electron-renderer#11 |
sure! for this project, no, since I use code snippet for the main thread: const electron = require('electron');
// ...
const ipcMain = electron.ipcMain;
function createInputBrowserWIndow(eventObj) {
let addCountdownWindow = new BrowserWindow({
// ...
});
const webContents = addCountdownWindow.webContents;
webContents.on('did-finish-load', () => {
// ...
webContents.send('eventData', eventInfo);
});
}
ipcMain.on('editOrCreate', function(ev, arg) {
// ...
ev.sender.send('updated', returnMsg);
}); code snippet for the renderer thread ( const ipcRenderer = require('electron').ipcRenderer;
ipcRenderer.on('eventData', function(event, msg) {
// ...
});
function submitForm(e) {
e.preventDefault();
// ...
ipcRenderer.send('editOrCreate', JSON.stringify(updatedData.concat([eventId])));
ipcRenderer.on('updated', (ev, msg) => {
alert(msg);
window.close();
});
} |
Accessing |
per commit electron/electron@eac2e7c remote needs to be required as require('electron').remote see discussion here: electron/electron#3755 should alleviate concerns in this discussion: hainproject#77
I have had the same issue, but my project is basically following all the things here. // main.js
// Modules to control application life and create native browser window
const {
app,
BrowserWindow
} = require('electron')
const path = require('path')
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
frame: false,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true, // Enables Node.js integration in the renderer process
contextIsolation: false, // Enables the Node.js integration to be able to load remote scripts
enableRemoteModule: true,
preload: path.join(__dirname, 'preload.js'),
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
mainWindow.setAlwaysOnTop(true, "screen-saver") // - 2 -
mainWindow.setVisibleOnAllWorkspaces(true)
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here. // renderer.js
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
const { remote } = require('electron');
const e = require('electron');
console.log(remote);
console.log(e)
const win = remote.getCurrentWindow(); /* Note this is different to the
html global `window` variable */
window.onbeforeunload = (event) => {
/* If window is reloaded, remove win event listeners
(DOM element listeners get auto garbage collected but not
Electron win listeners as the win is not dereferenced unless closed) */
win.removeAllListeners();
}
function handleWindowControls() {
// Make minimise/maximise/restore/close buttons work when they are clicked
document.getElementById('minimize-btn').addEventListener("click", event => {
win.minimize();
console.log('minimise');
});
document.getElementById('close-btn').addEventListener("click", event => {
win.close();
console.log("close");
});
console.log('window controls');
} <!--index.html-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
<meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'">
<!-- include the css -->
<link rel="stylesheet" href="style.css">
<title>Hello World!</title>
</head>
<body>
<header id="titlebar">
<div id="drag-region">
<div id="closebutton">
<button id="close-btn">
<span class="close_circle"></span>
</button>
</div>
<div id="minimizebutton">
<button id="minimize-btn">
<span class="minimize_circle"></span>
</button>
</div>
<div id="fullscreenbutton">
<button id="fullscreen-btn-disabled">
<span class="fullscreen-circle"></span>
</button>
</div>
</div>
</header>
<script src="./renderer.js"></script>
</body>
</html> // preload.js
// EMPTY It has electron as an object, but remote doesnt exist.
|
Hi,
I'm trying to use the remote module but I'm getting an error when running my app:
App threw an error when running [TypeError: Cannot read property 'BrowserWindow' of undefined]
Here's a simplified version of my code:
Any idea what the problem can be?
Many thanks
The text was updated successfully, but these errors were encountered: