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

× TypeError: fs.existsSync is not a function #7

Closed
tudiantuan opened this issue Jul 10, 2020 · 10 comments
Closed

× TypeError: fs.existsSync is not a function #7

tudiantuan opened this issue Jul 10, 2020 · 10 comments

Comments

@tudiantuan
Copy link

Screen Shot 2020-07-10 at 10 15 31

@yhirose
Copy link
Owner

yhirose commented Jul 14, 2020

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

@yhirose yhirose closed this as completed Jul 14, 2020
@kunokdev
Copy link

kunokdev commented Dec 2, 2020

@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.

@yhirose
Copy link
Owner

yhirose commented Dec 2, 2020

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

@kunokdev
Copy link

kunokdev commented Dec 2, 2020

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.

@yhirose
Copy link
Owner

yhirose commented Dec 2, 2020

@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?

@kunokdev
Copy link

kunokdev commented Dec 2, 2020

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.

@stereosteve
Copy link

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

@kunokdev
Copy link

kunokdev commented Dec 7, 2020

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

@stereosteve
Copy link

stereosteve commented Dec 7, 2020

@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

@Romex91
Copy link

Romex91 commented Jan 12, 2021

@stereosteve, window.require is an ugly workaround.

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

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

5 participants