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've upgraded my application into Angularv6 and rxjsv6 succesfully. After I upgrade from 1.7.4 to 6.0.5, I start getting similar messages to:

Module not found: Error: Can't resolve 'stream' in

I execute:

npm install stream events buffer asn1 timers vm --save

And the application compiles. When rendering the app on browser the first error is:

Uncaught ReferenceError: global is not defined at Object../node_modules/buffer/index.js (index.js:43)

Which I fix by adding: (window as any).global = window; in polyfills.ts file. Then I get:

ReferenceError: global is not defined at Object../node_modules/buffer/index.js (index.js:43)>

TypeError: Cannot read property 'prototype' of undefined at inherits (inherits_browser.js:5) at Object../node_modules/hash-base/index.js (index.js:23) at __webpack_require__ (bootstrap:76) at Object../node_modules/md5.js/index.js (index.js:3) at __webpack_require__ (bootstrap:76) at Object../node_modules/create-hash/browser.js (browser.js:3) at __webpack_require__ (bootstrap:76) at Object../node_modules/crypto-browserify/index.js (index.js:4) at __webpack_require__ (bootstrap:76)

And now I don't know how to proceed...

Pilukina avatar May 29 '18 09:05 Pilukina

const fs = (<any>window).require("fs");

works for me on renderer side without any other modifications (angular5/6). No build-time checks though.

ghuser avatar May 31 '18 10:05 ghuser

@Pilukina How did you fix that issue? I'm facing the same problem after the update.

nikunjazova avatar Jun 13 '18 06:06 nikunjazova

My problem was the same:

ERROR in ./node_modules/....js
Module not found: Error: Can't resolve 'os' in '.../node_modules/...'

It helps me: https://github.com/browserify/browserify-handbook#browser-field

We've added browser field in package (fortunately it was our package) that causes an error

"browser": {
    "fs": false,
    "path": false,
    "os": false
}

just like here https://github.com/Microsoft/TypeScript/blob/master/package.json

slawiko avatar Jun 13 '18 07:06 slawiko

using webpack ... https://github.com/jeremyfa/yaml.js/issues/102#issuecomment-367229834

Artistan avatar Jun 26 '18 17:06 Artistan

I have the same problems with electron and angular 6, I added:

"browser": { "fs": false, "path": false, "os": false }

as @slawiko suggested, on electron package.json and everything worked fine, but this is a workaround only, there should be a fix in order to solve this.

sanchezpablo avatar Jun 27 '18 09:06 sanchezpablo

Why are people patching node_modules? This can be solved using tsconfig paths instead. Please see my PR to PeerTube for an example: https://github.com/Chocobozzz/PeerTube/pull/742

EDIT: OK this has problems with the final build, even though it works for dev build :-
EDIT 2: Never mind, the production build succeeds, I was on the wrong branch. This appears to be a good solution to this issue.

rezonant avatar Jun 28 '18 05:06 rezonant

Thanks @rezonant , that fixed it for me. I don't like patching node_modules either. My issue was only with fs being used by yamljs. I'm on Angular 6.0.4, CLI 6.0.7, Node 10.4.0, npm 6.1.0, yarn 1.7.0. Unfortunately, @slawiko 's workaround didn't work for me. I did the following:

package.json:

{
   "scripts": {
      "postinstall": "npm rebuild node-sass"
   }
}

tsconfig.json:

{
   "compilerOptions": {
      "paths": {
         "fs": [ "./global-shims" ]
      }
   }
}

Then, in a global-shims.ts file:

(window as any).global = window;

export const NOOP = 0;

kahboom avatar Jul 05 '18 03:07 kahboom

I'm sorry to say that we do not have support for the Electron platform currently in Angular CLI. We only support building browser apps, and while building browser apps the node native imports (like fs) are not available. You can read more about why we disabled these imports in https://github.com/angular/angular-cli/issues/9827.

filipesilva avatar Jul 19 '18 13:07 filipesilva

We do not need support for Electron. We only need the compiler to not fail when we include libraries which have been badly written to require fs for some marginal functionality which we wont be using from them. When you started enforcing this in 6 you made a serious breaking change which could be easily avoided if you allowed us to go around this and avoid the compiler error.

doivosevic avatar Jul 19 '18 13:07 doivosevic

@filipesilva isn't the CLI supposed to support angular universal, which could use the same node APIs that electron uses?

ocombe avatar Jul 19 '18 13:07 ocombe

Just found this https://github.com/angular-guru/electron-builder (not tested but looks great)

sgruhier avatar Aug 16 '18 10:08 sgruhier

Here's my workaround, works both in ng serve and ng build. https://github.com/angular/angular-cli/issues/9827#issuecomment-435350839

Nishkalkashyap avatar Nov 02 '18 12:11 Nishkalkashyap

Try ngx-electron. It solved issues like this for me.

mdg1019 avatar Nov 30 '18 23:11 mdg1019

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.

how imported handlebarjs.js in angular.json

zztiswb116 avatar Apr 09 '19 03:04 zztiswb116

I have the same issue in my React-Redux project.I have use npm install "fs".But when I import * as fs from 'fs',there's a error.... ERROR in ./src/features/pages/myOwnPage/myPage/list.js Module not found: Error: Can't resolve 'fs' in '/Users/xuxiaoqi/Desktop/work/myOwnTest/src/features/pages/myOwnPage/myPage'

Cythia828 avatar Apr 28 '19 09:04 Cythia828

I create a new angular project: ng new ngapp1. vesion: Angular CLI:8.0.2 Node: 10.16.0 OS: win32 x64

now, I modify the file : app.component.ts, add some code import * as fs from 'fs'; console.log(fs);

then I process the cmd: ng serve, I get a isssue: ./src/app/app.component.ts Module not found: Error: Can't resolve 'tls' in 'C:\Users\Administrator\ngapp1\src\app'

I think it's a simple issue.but i don't know how to fix it. and i find many people have the same question. Does anyone can help us? Give a detailed solution. when anyone else has the same question, we can do by this way. Thanks.

B40914109 avatar Jun 10 '19 23:06 B40914109

I struggled with this problem for a long time. But I believe the best way to get around this error once and for all is to use a custom webpack build.

I'm using this angular-builder to add the custom webpack config.

After adding the builder, if you're targeting electron you can do something like this:-

module.exports = {
    ...
    output: {
        globalObject: 'self',
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    target: 'electron-renderer'
}

If you're targeting web, you can do something like this:-

module.exports = {
    ...
    target: 'web',
    node: { fs: 'empty' }
}

This is not a perfect config, but once you add webpack to the build process, the world opens up to you for your specific use cases. Simply move to stack overflow to find an answer.

I've tested this builder with angular version 6, 7, 8 without any problems.

Nishkalkashyap avatar Jun 11 '19 04:06 Nishkalkashyap

Update: Solved with help from SO Answer

sataqi avatar Jul 25 '19 08:07 sataqi

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.