angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

[v6] Electron: Module not found: Error: Can't resolve 'fs'

Open marcj opened this issue 8 years ago • 40 comments

Versions

Angular CLI: 6.0.0
Node: 9.11.1
OS: darwin x64
Angular: 6.0.0
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, http, language-service, material, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.6.0
@angular-devkit/build-angular     0.6.0
@angular-devkit/build-optimizer   0.6.0
@angular-devkit/core              0.3.2
@angular-devkit/schematics        0.6.0
@ngtools/webpack                  6.0.0
@schematics/angular               0.6.0
@schematics/update                0.6.0
rxjs                              6.1.0
typescript                        2.7.2
webpack                           4.7.0

Repro steps

When I develop an electron app (and thus have access to Node's fs module), I can't access node modules because typescript or webpack isn't able to resolve it.

import * as fs from 'fs'

Observed behavior

I get an error while building the application ng build:

WARNING in ./node_modules/zone.js/dist/zone-mix.js
Module not found: Error: Can't resolve 'fs' in '/Users/marc/bude/project/node_modules/zone.js/dist'

ERROR in ./src/main/home.ts
Module not found: Error: Can't resolve 'os' in '/Users/marc/bude/project/src/main'

ERROR in ./src/main/home.ts
Module not found: Error: Can't resolve 'path' in '/Users/marc/bude/project/src/main'

....

Desired behavior

I'd like to be able to use Node's modules and define somewhere in the angular.json configuration to allow certain modules (in other words: exclude them from the build process, as the Node runtime provides that stuff).

Mention any other details that might be useful (optional)

In Angular 5 I was able to eject and add in the webpack.config.js following lines at the root level:

  "externals": {
      "mongodb": 'require("mongodb")',
      "fs": 'require("fs")',
  },

However since Anuglar 6 does not provide an eject anymore, I'm not able to use that old webpack.config.json from v5 anymore. To make Angular 6 workable in Electron I need to be able to use a webpack.config.js again or maybe a config option in angular.json that allows me to define webpack's externals.

marcj avatar May 06 '18 18:05 marcj

I cannot use handlebarjs for the same reason :( It's a blocker to upgrade to ng6

sgruhier avatar May 08 '18 10:05 sgruhier

I've been running into the same problem. Actually, I only need to use ng eject though to get externals to work and would prefer using angular-cli directly rather than having to eject. So, at least for me, an externals option for the angular-cli.json/angular.json would help a lot and might also be a quick fix for this specific problem.

jushar avatar May 09 '18 12:05 jushar

I've been running into the same problem. For ng serve, I get an error:

ERROR in ./node_modules/content-disposition/index.js
Module not found: Error: Can't resolve 'path' in 'D:\Sources\SSIV\Projects\Portal\angular\node_modules\content-disposition'

As a temporary solution, I changed var basename = require('path').basename to var basename = require('path-browserify').basename in the module content-disposition.

b0bi79 avatar May 09 '18 12:05 b0bi79

I suspect I'm getting the same error using libsodium-sumo. After upgrading to angular 6 I get

ERROR in ./node_modules/libsodium-sumo/dist/modules-sumo/libsodium-sumo.js
Module not found: Error: Can't resolve 'path' in '/home/david/Projects/passit-frontend/node_modules/libsodium-sumo/dist/modules-sumo'

Should be easily reproducable on this branch.

Frustratingly, the path module isn't used at all in libsodium.js when used in a browser - it exists only when used in node. It's presence seems to trigger this error.

If I delete references to path, everything works including libsodium crypto.

bufke avatar May 09 '18 18:05 bufke

Same issue... After attempting to upgrade to angular-cli v6, I get the following when attempting to run ng serve:

WARNING in ./node_modules/sax/lib/sax.js
Module not found: Error: Can't resolve 'stream' in '/Users/alockwood/ShapeShifter/node_modules/sax/lib'

ERROR in ./node_modules/svgo/lib/svgo/js2svg.js
Module not found: Error: Can't resolve 'os' in '/Users/alockwood/ShapeShifter/node_modules/svgo/lib/svgo'
ERROR in ./node_modules/jszip/lib/readable-stream-browser.js
Module not found: Error: Can't resolve 'stream' in '/Users/alockwood/ShapeShifter/node_modules/jszip/lib'

alexjlockwood avatar May 10 '18 01:05 alexjlockwood

Same issue here when using ng serve

WARNING in ./node_modules/xml2js/node_modules/sax/lib/sax.js
Module not found: Error: Can't resolve 'stream' in 'C:\Develop\djjb-news-reader\node_modules\xml2js\node_modules\sax\lib'

ERROR in ./node_modules/xml2js/lib/parser.js
Module not found: Error: Can't resolve 'timers' in 'C:\Develop\djjb-news-reader\node_modules\xml2js\lib'

ckruhs avatar May 10 '18 12:05 ckruhs

I've found a "temporary" hack to make this work here: https://github.com/angular/angular-cli/issues/4227#issuecomment-310811078

You can also use window.require('fs') or any other node module, that should work

ocombe avatar May 10 '18 13:05 ocombe

Did you try it? I tried that and I still get Module not found: Error: Can't resolve 'fs' in '..../node_modules/handlebars/lib';

sgruhier avatar May 10 '18 13:05 sgruhier

I guess it depends how your lib imports its dependencies, it works for electron and fs, but I just tried rxdb and the "pouchdb-adapter-localstorage" that imports "stream" fails :(

ocombe avatar May 10 '18 13:05 ocombe

My issue was solved by using npm install events buffer stream timers --save

Tip found here: https://stackoverflow.com/questions/50234196/after-updating-from-angular-5-to-6-i-keep-getting-the-error-cant-resolve-timer

ckruhs avatar May 10 '18 14:05 ckruhs

Same error here with fs and lokijs

gimox avatar May 10 '18 19:05 gimox

Same error with path here

YoannBureau avatar May 15 '18 08:05 YoannBureau

Same here with path or fs and other node modules for a browser application. Some external dependencies require these modules (and check they exist at runtime) so we cannot upgrade our application to Angular 6.

These changes in this commit seem to be responsible for it: https://github.com/angular/devkit/commit/8e7658aabc71829153e0dfccf60cc526142a16dd#diff-085e357d25d94ae495a662c157178fb0L103

Chocobozzz avatar May 15 '18 12:05 Chocobozzz

This should be address as soon as possible. It blocks many people on ng6 upgrade. In my case with handlebarjs, I imported handlebarjs.js in angular.json instead of using an import in my code. Not very sexy but at least it works.

sgruhier avatar May 15 '18 12:05 sgruhier

By the way this is the reasoning behind this change https://github.com/angular/angular-cli/issues/9827#issuecomment-369578814 and the relevant PR https://github.com/angular/angular-cli/pull/9812 that is later ported in https://github.com/angular/devkit/commit/8e7658aabc71829153e0dfccf60cc526142a16dd#diff-085e357d25d94ae495a662c157178fb0L103

I really hope there would be a reasonable solution - for now the best I can figure out is an option to override / supplement webpack configuration so that nodejs shimming can be defined per project.

Workaround (the best one I found, thanks @niespodd): https://gist.github.com/niespodd/1fa82da6f8c901d1c33d2fcbb762947d

smoke avatar May 15 '18 13:05 smoke

Workaround: I've found a workaround that works for me: I downgraded angular-cli to version 1.7.4 and use there ng eject (which works for Angular 6 surprisingly). and then adjust webpack.config.js accordingly.

marcj avatar May 15 '18 13:05 marcj

Great you find a fix for you I'm not a huge fan of downgrading to CLI 1.7.4. And using eject just because of that issue doesn't look a good solution. Now you are stuck outside CLI. I hope it will be fix soon.

sgruhier avatar May 15 '18 13:05 sgruhier

@smoke I completely understand the reasoning behind the change. But I don't understand why the Angular-CLI team did not provide a workaround (like an override option for example). We have 10 dependencies that rely on node modules (fs, path etc). How are we supposed to do? Fix all the dependencies ourselves? Staying on Angular CLI 1.7? Waiting an eject option?

I am very thankful to the Angular team to provide us an awesome framework with helpful tools (:heart:), but I spent many hours of my free time just to upgrade 1 element (Angular) of our application... and finally realize that we are at a dead end.

Maybe going back to Angular CLI 1.7 is the best solution for now, but please think about developers that just want to upgrade their application without running into a wall :)

Chocobozzz avatar May 15 '18 14:05 Chocobozzz

My current (hacky) workaround is running this as a postinstall script:

sed -i 's/require("path")/require("something-i-know-exists")/g' node_modules/some-package/file-error-is-in.js

In my case I don't use the node import that errors because my code only runs in a browser. So it can import the wrong package but who cares. This allows me to keep using the cli without ejecting webpack. It is of course a horrible, horrible hack.

bufke avatar May 15 '18 14:05 bufke

I have placed one good workaround in my post https://github.com/angular/angular-cli/issues/10681#issuecomment-389160125

smoke avatar May 15 '18 14:05 smoke