maximegris/angular-electron

Error: Module not found: Error: Can't resolve 'fs'

Sarthhak03 opened this issue · 3 comments

Describe the bug
Can not access 'fs' filesystem

To Reproduce
Steps to reproduce the behavior:

  1. in a service file , writing a logic to read files , for that did-
  2. import * as fs from 'fs';
  3. fs.readFileSync()
  4. getting error - ./node_modules/electron/index.js:1:11-24 - Error: Module not found: Error: Can't resolve 'fs'

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context

  • angular 15
  • electron v-24
  • node 16.20.0

Add any other context about the problem here.

Hi
Please check providers/electron.service.ts to watch how conditional import of libraries has to be done when using NodeJS / 3rd party libraries in renderer context (i.e. Angular).

@maximegris

./node_modules/electron/index.js:1:11-24 - Error: Module not found: Error: Can't resolve 'fs' i

Why is the error appearing inside of index.js in node_modules?
the 'fs' of getElectronPath:

const fs = require('fs');
const path = require('path');

const pathFile = path.join(__dirname, 'path.txt');

function getElectronPath () {
  let executablePath;
  if (fs.existsSync(pathFile)) {
    executablePath = fs.readFileSync(pathFile, 'utf-8');
  }
  if (process.env.ELECTRON_OVERRIDE_DIST_PATH) {
    return path.join(process.env.ELECTRON_OVERRIDE_DIST_PATH, executablePath || 'electron');
  }
  if (executablePath) {
    return path.join(__dirname, 'dist', executablePath);
  } else {
    throw new Error('Electron failed to install correctly, please delete node_modules/electron and try installing again');
  }
}

module.exports = getElectronPath();

I can't see where my application code is importing fs, how can I find it?

The readme says:

NodeJS's one - Uses NodeJS core module (crypto, fs, util...)
I suggest you add this kind of 3rd party library in dependencies of both app/package.json and package.json (root folder) in order to make it work in both Electron's Main process (app folder) and Electron's Renderer process (src folder).

But you can't actually add 'fs' or any of those core modules into package.json.

One possibility is i'm important and using a module that itself is importing fs somewhere inside it; but i can't figure out which one.

Fixed.
For future reference:

I found the code that was causing the problem:

Inside mmy source folder, somewhere in my angular app I was doing:

    ipcRenderer.on('scanner-message', (event, message) => {
      console.log('gotthemessage', message);
      // Do something with the message, like updating the UI
    });

Instead, what you're SUPPOSED to do; is import the provided electronService:

constructor(private electron: ElectronService) {}

and then use THAT thing's ipcRenderer, (it also has fs, and a few other things, i suppose the idea behind this service that wasn't clear to me at the start was that this is a helper service that allows 'safe' access to nodeJs things, without blowing up when running on the browser that doesn't have access to nodeJS).

    this.electron.ipcRenderer.on('scanner-message', (event, message) => {
      console.log('gotthemessage', message);
      // Do something with the message, like updating the UI
    });

I understand now.

Thank you for the project; Very nice.