ThorstenHans / ngx-electron

A simple Angular wrapper for electron's Renderer API
MIT License
429 stars 86 forks source link

isElectronApp is true but ipcRenderer is null #32

Closed pilz97 closed 5 years ago

pilz97 commented 5 years ago

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 commented 5 years ago

I have the same problem. Reproduction scenario:

ThorstenHans commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

which version of ng and electron are you using

hjvanranden commented 5 years ago

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

bz-dev commented 5 years ago

Try setting nodeIntegration to true.

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

hjvanranden commented 5 years ago

That fixes the problem, thank you very much!!

pilz97 commented 5 years ago

Thank you fixes the problem for me too! :)

Bug-Reaper commented 4 years ago

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

venomoustoad commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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 commented 1 year ago

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'),
      },
    });