Giter Club home page Giter Club logo

Comments (10)

stereosteve avatar stereosteve commented on May 18, 2024 2

@kunokdev I see that using window.require("fs") instead of using import works, but all the TypeScript typing is missing. Is there a way to get the typing for node modules as well?


update this seems to work ok:

import * as FS from "fs";
const fs: typeof FS = window.require("fs");

or:

import { IpcRenderer } from 'electron'
const ipcRenderer: IpcRenderer = window.require('electron').ipcRenderer

from react-typescript-electron-sample-with-create-react-app-and-electron-builder.

yhirose avatar yhirose commented on May 18, 2024 1

@tudiantuan, thank you for the report. But, I don't find such a problem at all.

from react-typescript-electron-sample-with-create-react-app-and-electron-builder.

kunokdev avatar kunokdev commented on May 18, 2024 1

@yhirose this is common problem when trying to import ipcRenderer into React code.
Seems like this repository doesn't handle/support ipc communication between renderer and main process.

from react-typescript-electron-sample-with-create-react-app-and-electron-builder.

yhirose avatar yhirose commented on May 18, 2024

@kunokdev, great! Could you show me the smallest possible code to reproduce it?

from react-typescript-electron-sample-with-create-react-app-and-electron-builder.

kunokdev avatar kunokdev commented on May 18, 2024

Steps to reproduce:

  1. Clone this repo
  2. Open any React file, e.g. App.tsx
  3. Try to import anything electron related

import { ipcRenderer } from "electron";

const App: React.FC = () => {
  console.log(ipcRenderer);

This error appears:

image

Perhaps changing import to:

const ipcRenderer = window.require("electron").ipcRenderer;

could solve this problem.

from react-typescript-electron-sample-with-create-react-app-and-electron-builder.

yhirose avatar yhirose commented on May 18, 2024

@kunokdev, do you think if it's a problem related to this project, or a typical Electron issue which can happen in any Electron project?

from react-typescript-electron-sample-with-create-react-app-and-electron-builder.

kunokdev avatar kunokdev commented on May 18, 2024

I'd say It's caused by electron+cra(webpack) combo; Depends how you wanna look at it; this issue affects this repo; but this repo isn't the cause of the issue. You should probably add something in README or code example where renderer (React) process communicates with main (Electron) process.

from react-typescript-electron-sample-with-create-react-app-and-electron-builder.

stereosteve avatar stereosteve commented on May 18, 2024

To use node modules and code in react, the webpack target needs to be electron-renderer.

CRA doesn't make it easy to modify webpack config, options are:

  • eject
  • add a preload script, and use window.postMessage in react code to send message to preload code, and use ipcRenderer from there. This is described in this article

from react-typescript-electron-sample-with-create-react-app-and-electron-builder.

kunokdev avatar kunokdev commented on May 18, 2024

To use node modules and code in react, the webpack target needs to be electron-renderer.

CRA doesn't make it easy to modify webpack config, options are:

  • eject
  • add a preload script, and use window.postMessage in react code to send message to preload code, and use ipcRenderer from there. This is described in this article

I am successfully using ipcRenderer without ejecting or preload scripts

from react-typescript-electron-sample-with-create-react-app-and-electron-builder.

Romex91 avatar Romex91 commented on May 18, 2024

@stereosteve, window.require is an ugly workaround.

There is a better fix:
electron/electron#7300 (comment)

from react-typescript-electron-sample-with-create-react-app-and-electron-builder.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.