How to load an electronic module in typescript

I am trying to get the ipcRenderer module from an electron in typescript to send information from the current component to the kernel and get the information back to the window (electron-chrome browser). All I get is a "Module not found" error, recoding ts code to ES5.

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

Update: Error switching between "Module not found" and this:

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

This is from the current electron api . I also tried using the import syntax from typescript, but the result is the same.

Than I tried to use the electronic.ipcRenderer module in the ES5 file loaded / linked directly in the html file.

There it worked. Why?

+5
source share
3 answers

Since the electronic dependency in the browser is not real, that is, it is not overloaded from the web page from node_modules, but instead loaded at run time, the require statement causes errors, such as "fs", which are not found for me.

However, you can trick typescript like 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 application that is only supplemented by an electron when it works internally, this is the best way, since you do not need to add an electron depending on your webapp only when using communication parts.

+2
source

Than I tried to use the electronic.ipcRenderer module in the ES5 file loaded / linked directly in the html file.

If it works in html, but a failure in ts means that the error is not in const ipc = require('electron').ipcRenderer; . The error is most similar to import, which you should load the file from html (and not require('electron') ).

+1
source

Source: https://habr.com/ru/post/1240031/


All Articles