Skip to content
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 run ngx-monaco-editor with electron #758

Closed
isNotOkay opened this issue Jun 20, 2023 · 1 comment
Closed

Unable to run ngx-monaco-editor with electron #758

isNotOkay opened this issue Jun 20, 2023 · 1 comment

Comments

@isNotOkay
Copy link

isNotOkay commented Jun 20, 2023

Hello,

I am trying to run ngx-monaco-editor in electron but I get the following error:

in.ts:13 ERROR Error: Uncaught (in promise): TypeError: window.require.config is not a function
TypeError: window.require.config is not a function
    at onGotAmdLoader (ngx-monaco-editor-v2.mjs:44:36)
    at ngx-monaco-editor-v2.mjs:62:21

Did anyone manage do this?

Setting nodeIntegration to true gets rid of the error:

  // Create the browser window.
  win = new BrowserWindow({
    x: 0,
    y: 0,
    width: size.width,
    height: size.height,
    webPreferences: {
      nodeIntegration: true
    },
  });

Unfortunately this results in ipcRenderer being undefined and electronService.isElectron being false when opening the app in electron:

  constructor(
    private electronService: ElectronService
  ) {
    electronService.isElectron // false
    electronService.ipcRenderer // undefined
}

Update:

I fixed this issue by adding a preload.js script:

window.isElectron = true;
window.ipcRenderer = require('electron').ipcRenderer;
window.webFrame = require('electron').webFrame;

The electron.service.ts has to be adjusted like this:

constructor() {
    // Conditional imports
    if (this.isElectron) {
      // @ts-ignore
      this.ipcRenderer = window.ipcRenderer;
      // @ts-ignore
      this.webFrame = window.webFrame;
    }
  }

  get isElectron(): boolean {
    // @ts-ignore
    return window.isElectron;
  }

Make sure to load the preload script in app\main.ts and that contextIsolation is set to false, otherwise the values of the preload script are not accessible on the window object:

  // Create the browser window.
  win = new BrowserWindow({
    x: 0,
    y: 0,
    width: size.width,
    height: size.height,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      contextIsolation: false,
    },
  });
@maximegris
Copy link
Owner

Hi
I close the issue because the BrowserWindow default configuration of this project already have noteIntegration setted to true.

new BrowserWindow({
    x: 0,
    y: 0,
    width: size.width,
    height: size.height,
    webPreferences: {
      nodeIntegration: true,
      allowRunningInsecureContent: (serve),
      contextIsolation: false,
    },
  });

So if you had to set nodEIntegration to true by yourself, that means you made changes on your project that may cause the encounter issue.
Please read #752 (comment) if you want to add preload script with nodeIntegration: false

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

No branches or pull requests

2 participants