7

I try to get the ipcRenderer module from electron in typescript to send informations from the current component to the core and to get informations back to the window (electron-chromium-browser). All I get is a error "Module not found" by transcoding the ts-code to ES5.

const ipc = require('electron').ipcRenderer;`

Update: The Error is switching between the "Module not found" and this one:

ERROR in ./~/electron/index.js Module build failed: Error: ENOENT, open '/.../node_modules/electron/index.js' @ ./app/components/search/search.ts 12:10-29

That is from the current electron-api. I have also tried to use the import syntax from typescript but the result is the same.

Than I tried to use the electron.ipcRenderer module in a ES5-file, loaded/linked directly in the html-file.

There it worked. Why?

2
  • have you tried using the electron-compile module that has support for TS and ES6 and all? Commented Jan 7, 2016 at 7:27
  • Another Q/A solved my issue: stackoverflow.com/questions/62467092/… Commented Apr 11, 2021 at 4:30

5 Answers 5

5

Solved the problem after 10h searching. Problem was the webpack-transcoder.

https://github.com/chentsulin/webpack-target-electron-renderer

https://github.com/chentsulin/electron-react-boilerplate/blob/master/webpack.config.development.js

Sign up to request clarification or add additional context in comments.

2 Comments

can you explain a little bit more how you fixed it please? :)
in my case i use webpack to transcode typescript code to es5, in that way I have a webpack.config.js-File. In that config you have to send the hole config through webpackTargetElectronRenderer(config) to config.target. it should looks like that: var config = {.....}; config.target = webpackTargetElectronRenderer(config); module.exports = config;
3

Since electron dependency in the browser app is not real, meaning it's not webpacked from node_modules but instead loaded in runtime, the require statement caused errors such as "fs" not found for me.

However you can trick the typescript with this:

if (typeof window['require'] !== "undefined") { let electron = window['require']("electron"); let ipcRenderer = electron.ipcRenderer; console.log("ipc renderer", ipcRenderer); }

Also if you are writing a web app, which only is augmented by electron when it's running inside, this is a better way since you don't have to add electron as a dependency to your webapp just when using the communication parts.

2 Comments

Thank you, this is not obvious an under documented. I've been searching for hours for this. I feel there's probably a more elegant solution but this gets me moving again.
I don't know why this works when await import('electron') doesn't, but after spending all day on this I'm not going to question it
1

Than I tried to use the electron.ipcRenderer module in a ES5-file, loaded/linked directly in the html-file.

If it works in html but fails in ts it means the error is not in const ipc = require('electron').ipcRenderer;. The error is most likey in the import you have to load your file from html (and not require('electron')).

Comments

0

This solved the problem for me:

You can fix it, just add to the "package.json"

"browser": {
"fs": false,
"path": false,
"os": false }

Source: https://github.com/angular/angular-cli/issues/8272#issuecomment-392777980

Comments

0

You can trick ts with this (dirty hack, but it works):

const { ipcRenderer } = (window as any).require("electron");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.