Search code examples
javascriptreact-nativeelectronelectron-builderreact-native-web

Importing electron conditionally into react-native-web app


I currently have a react-native-web app that targets both Android and Electron.

I've added electron shortcuts into the app and I'm using the ipcRenderer in react components to listen to accelerators and run react-specific code.

The problem is, unless I am importing the ipcRenderer into a .web.js file I receive the following error when running Android:

Unable to resolve module `fs` from `/Users/.../node_modules/electron/index.js`: Module `fs` does not exist in the Haste module map

Snippet of code causing the problem:

if (isElectron()) {
    const { ipcRenderer } = require('electron'); <-- this line

I've tried wrapping the import in a Platform.OS conditional like so

if (Platform.OS === 'web') {
    const isElectron = require('is-electron');

    if (isElectron()) {
        const { ipcRenderer } = require('electron');

However I still receive the same error.

Thank you :)


Solution

  • Found a solution to my problem :)

    I made 2 files

    ipcRenderer.js containing an export of a null object

    and ipcRenderer.web.js which contained the isElectron() check and either exported a null or the ipcRenderer from electron