question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't resolve 'fs'

See original GitHub issue

With Angular 9 I get the following error:

ERROR in ../node_modules/electron/index.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\feren\Projects\[projectname]\node_modules\electron'
ERROR in ../node_modules/electron-log/src/transports/file/index.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\feren\Projects\[projectname]\node_modules\electron-log\src\transports\file'
ERROR in ../node_modules/electron-log/src/transports/file/file.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\feren\Projects\[projectname]\node_modules\electron-log\src\transports\file'
ERROR in ../node_modules/electron-log/src/transports/file/packageJson.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\feren\Projects\[projectname]\node_modules\electron-log\src\transports\file'

relevant part of app.module.ts:

import * as log from 'electron-log';

log.transports.file.level = false;
log.transports.ipc.level = 'info';
console.log = log.log;

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
bno1commented, Jul 4, 2021

I found a solution, follow this guide: https://developer.okta.com/blog/2019/12/09/angular-webpack (just the Set Up Angular With Webpack section), and in your custom-webpack.config.js set the target to electron-renderer, like so:

module.exports = {
    target: 'electron-renderer',
};
0reactions
greg9504commented, Feb 16, 2022

If you get the "Error: Can’t resolve ‘fs’ in … " in your Angular application after adding Electron-log try this:

In the renderer (a component) use code like this (note the window.require):

import { Component } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import log from 'electron-log';  //<--

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {

  private elog: typeof log; //<--

  constructor(
    private electronService: ElectronService,
    private translate: TranslateService
  ) {
    this.translate.setDefaultLang('en');
    
    this.elog = window.require('electron-log');  //<--
    this.elog.info('Hello, log'); // use it

  }
}

If using the maximegris/angular-electron starter you would probably want to put the instance of log in the ElectronService and access it through that, so you don’t have to write the window.require everywhere.

More info https://github.com/maximegris/angular-electron/issues/632#issuecomment-898484389 and https://github.com/maximegris/angular-electron#how-to-import-3rd-party-libraries

In main process just instantiate it normally and make sure to set nodeIntegration: true, and contextIsolation: false if you want to use Electron-log in renderer process.

/app/main.ts

import { app, BrowserWindow, screen } from 'electron';
import log from 'electron-log';

log.info('In main process');

....

win = new BrowserWindow({
    x: 0,
    y: 0,
    width: size.width,
    height: size.height,
    webPreferences: {
      nodeIntegration: true,
      allowRunningInsecureContent: (serve) ? true : false,
      contextIsolation: false,  
    },
  });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Module not found: Error: Can't resolve 'fs' in - Stack Overflow
I found a possible solution, that you have to put some configuration in one of the node_modules. But I think that is not...
Read more >
How To Solve Module Not Found Can't Resolve 'fs' in Next.js
The Module not found : Can't resolve 'fs' error and similar issues most likely occur when you try to import a module that...
Read more >
Can't resolve 'fs' error in Next.js and WebPack - Nsikak Imoh
The Module not found: Can't resolve 'fs' in Next.js error occurs when you import a Node.js module that is not available in the...
Read more >
Module not found: Can't resolve 'fs' error [Solved] | bobbyhadz
The error "Module not found: Error: Can't resolve 'fs'" occurs because there has been a breaking change in Webpack version 5. To solve...
Read more >
Can't resolve 'fs' when bundle with webpack #447 - GitHub
I got this error when using the "fetch" library from npm. At first I got the same error but with other packages (...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found