Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

isElectronApp is true but ipcRenderer is null #32

Closed
pilz97 opened this issue Apr 26, 2019 · 13 comments
Closed

isElectronApp is true but ipcRenderer is null #32

pilz97 opened this issue Apr 26, 2019 · 13 comments

Comments

@pilz97
Copy link

pilz97 commented Apr 26, 2019

Hi,

i packed my angular app to an .exe with electron-packager.
Inside my app I want to use sendSync from the ipcRenderer, but then I get an error because the ipcRenderer is null although isElectronApp is true..

This is my code where the window is created:

`
const { app, BrowserWindow } = require('electron');
const path = require('path');
const url = require('url');

let win;

const createWindow = () => {
win = new BrowserWindow({
width: 800,
height: 600,
icon: path.join(__dirname, 'assets/test.png'),
});
win.setKiosk(true);

win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));

win.on('closed', () => {
win = null;
});
}

app.on('ready', createWindow);

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});

app.on('activate', () => {
if (win === null) {
createWindow();
}
});
`

What I am doing wrong?

@hjvanranden
Copy link

hjvanranden commented Apr 28, 2019

I have the same problem.
Reproduction scenario:

  • Clone https://github.com/hjvanranden/angular-electron
  • Checkout branch with_ngx-electron_problem_ipcRenderer_is_null
  • In root directory on command prompt:
    npm install
    npm run electron-build
  • In electron window it says in the developer console:
    this.electronSvc.ipcRenderer = null

@ThorstenHans
Copy link
Owner

@hjvanranden what was the issue? Sounds like you investigated already some time to get that up and running. Maybe something that could be contributed back to ngx-electron

@hjvanranden
Copy link

The issue is that when using Angular in Electron (with ngx-electron), I cannot make an IPC call, because ipcRenderer == null (although electronSvc.isElectronApp returns true).

@ThorstenHans
Copy link
Owner

which version of ng and electron are you using

@hjvanranden
Copy link

Angular CLI: 7.3.8
"electron": "^5.0.0",

@bz-dev
Copy link

bz-dev commented May 1, 2019

Try setting nodeIntegration to true.

new BrowserWindow({ webPreferences: { nodeIntegration: true } });

@hjvanranden
Copy link

That fixes the problem, thank you very much!!

@pilz97
Copy link
Author

pilz97 commented May 2, 2019

Thank you fixes the problem for me too! :)

@pilz97 pilz97 closed this as completed May 2, 2019
@Bug-Reaper
Copy link

Bug-Reaper commented Jul 21, 2020

Fixed for me, would be good to have some documentation on it! thanks @bz-dev

@venomoustoad
Copy link

So I have tried this with electron v12 - I need to do 3 things:
nodeIntegration: true, enableRemoteModule: true, contextIsolation: false
As a consequence I am able to access electronService.remote.fs etc etc.
however, when i set contextIsolations as false, it breaks my giving other errors in the angular code:
ReferenceError: $ is not defined
My jquery etc, which was normally preloaded earlier does not load any more. Why is this happening

@danielwarke
Copy link

@venomoustoad I am running into a similar issue with electron v12. The reason is that turning on nodeIntegration causes an issue when running jQuery or angularJS: https://www.electronjs.org/docs/faq#i-can-not-use-jqueryrequirejsmeteorangularjs-in-electron
Using the delete window properties solution that is shown in the electron FAQ is not solving the issue with the icpRenderer. You basically have to choose between using jQuery and using icpRenderer. I am struggling with this when using a child window to open up an oauth webpage from a 3rd party website where they require jQuery.

@danielwarke
Copy link

danielwarke commented Sep 10, 2021

Actually I just solved my issue! By using a conditional to check what page the preload script is running on, I was able to delete the node window properties but only on the child window. Keeping these properties on the parent window is required for icpRenderer to work in that context, but I do not need icpRenderer in the child window. Hope this helps!

@MrTob
Copy link

MrTob commented Sep 29, 2023

I have actually the same problem, when i set the contextIsolation: false it is working - but Electron throws a warning/error "Error: contextBridge API can only be used when contextIsolation is enabled"
When i set the contextIsolation to true the IPC calls are not working
@ThorstenHans

    App.mainWindow = new BrowserWindow({
      width: width,
      height: height,
      show: true,
      icon: join(__dirname, '..', electronAppName, 'assets', 'icons', 'icon.png'),
      webPreferences: {
        contextIsolation: false,
        nodeIntegration: true,
        backgroundThrottling: false,
        preload: join(__dirname, 'main.preload.js'),
      },
    });

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants