How to solve this error Module not found: Error: Can't resolve 'fs'
Asked Answered
B

2

14

I have created new angular project with electron.I do need to set up the electron.

I have got below errors.

ERROR in ./node_modules/electron/index.js

Module not found: Error: Can't resolve 'fs' in 'D:\PATH\desktop\node_modules\electron'

My package.json file

{
  "name": "desktop",
  "version": "0.0.0",
  "main": "main.js",
  "scripts": {
          "ng": "ng",
          "start": "ng serve",
          "build": "ng build",
          "test": "ng test",
          "lint": "ng lint",
          "e2e": "ng e2e",
          "electron": "ng build && electron .",
          "electron-aot": "ng build --prod && electron ."
        },
  "private": true,
  "dependencies": {
    "@angular/animations": "~7.1.0",
    "@angular/common": "~7.1.0",
    "@angular/compiler": "~7.1.0",
    "@angular/core": "~7.1.0",
    "@angular/forms": "~7.1.0",
    "@angular/platform-browser": "~7.1.0",
    "@angular/platform-browser-dynamic": "~7.1.0",
    "@angular/router": "~7.1.0",
    "core-js": "^2.5.4",
    "rxjs": "~6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.11.0",
    "@angular/cli": "~7.1.1",
    "@angular/compiler-cli": "~7.1.0",
    "@angular/language-service": "~7.1.0",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^8.9.5",
    "codelyzer": "~4.5.0",
    "electron": "^4.1.4",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.1.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.1.6"
  }      
}
Bukovina answered 21/4, 2019 at 3:58 Comment(2)
Please refer to this issue on github: github.com/angular/angular-cli/issues/10681Illconditioned
for me this solution worked: github.com/angular/angular-cli/issues/9827Septicidal
N
0

The error is because angular-CLI does not support modules in Nodejs like "fs" and "path".

Add the following to the root of the "package.json" file.

  "name": "testapp",
  "version": "1.0.0",
  "requires": true,
  ...
  "browser": {
    "fs": false,
    "path": false,
    "os": false
  }
...
}

this solution may be helpful.

Nickey answered 19/3, 2022 at 6:24 Comment(1)
That solution doesn't work if you WANT to use this functionality.Lacreshalacrimal
B
0

I know it's an old question, but...

This error happens when you try to compile Node.js code with webpack (which is used by Angular CLI), and that it is not configured for it. This usually means you require/import something from electron, or try to use Node.js APIs in your renderer process.

It's important to know that using Node.js in the renderer process is a security risk. Instead of trying to tweak your webpack configuration, you should consider following security recommendations and use a preload file and IPC to do what you need with Node.js stuff.

It should also be noted that if you get this error when trying to bundle your main and/or preload files, then it means you need to configure webpack with the correct target: electron-main and electron-preload respectively.

Bytom answered 30/12, 2023 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.