64

I'm building an Angular 6 app, but every time I want to serve to localhost, I get these errors:

ERROR in ./node_modules/aws-sign2/index.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\aws-sign2'
ERROR in ./node_modules/aws4/aws4.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\aws4'
ERROR in ./node_modules/ecc-jsbn/index.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\ecc-jsbn'
ERROR in ./node_modules/http-signature/lib/signer.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\http-signature\lib'
ERROR in ./node_modules/http-signature/lib/verify.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\http-signature\lib'
ERROR in ./node_modules/oauth-sign/index.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\oauth-sign'
ERROR in ./node_modules/request/lib/oauth.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\request\lib'
ERROR in ./node_modules/request/lib/helpers.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\request\lib'
ERROR in ./node_modules/request/lib/hawk.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\request\lib'
ERROR in ./node_modules/sshpk/lib/signature.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/private-key.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/certificate.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/fingerprint.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/key.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/dhe.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/identity.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/utils.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/sshpk/lib/formats/pem.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib\formats'
ERROR in ./node_modules/sshpk/lib/formats/ssh-private.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib\formats'
ERROR in ./node_modules/sshpk/lib/formats/openssh-cert.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib\formats'
ERROR in ./node_modules/request/lib/har.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\sorou\projects\tunrWeb\node_modules\request\lib'
ERROR in ./node_modules/forever-agent/index.js
Module not found: Error: Can't resolve 'http' in 'C:\Users\sorou\projects\tunrWeb\node_modules\forever-agent'
ERROR in ./node_modules/http-signature/lib/signer.js
Module not found: Error: Can't resolve 'http' in 'C:\Users\sorou\projects\tunrWeb\node_modules\http-signature\lib'
ERROR in ./node_modules/request/request.js
Module not found: Error: Can't resolve 'http' in 'C:\Users\sorou\projects\tunrWeb\node_modules\request'
ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'http' in 'C:\Users\sorou\projects\tunrWeb\node_modules\tunnel-agent'
ERROR in ./node_modules/forever-agent/index.js
Module not found: Error: Can't resolve 'https' in 'C:\Users\sorou\projects\tunrWeb\node_modules\forever-agent'
ERROR in ./node_modules/request/request.js
Module not found: Error: Can't resolve 'https' in 'C:\Users\sorou\projects\tunrWeb\node_modules\request'
ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'https' in 'C:\Users\sorou\projects\tunrWeb\node_modules\tunnel-agent'
ERROR in ./node_modules/forever-agent/index.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\sorou\projects\tunrWeb\node_modules\forever-agent'
ERROR in ./node_modules/tough-cookie/lib/cookie.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\sorou\projects\tunrWeb\node_modules\tough-cookie\lib'
ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\sorou\projects\tunrWeb\node_modules\tunnel-agent'
ERROR in ./node_modules/mime-types/index.js
Module not found: Error: Can't resolve 'path' in 'C:\Users\sorou\projects\tunrWeb\node_modules\mime-types'
ERROR in ./node_modules/assert-plus/assert.js
Module not found: Error: Can't resolve 'stream' in 'C:\Users\sorou\projects\tunrWeb\node_modules\assert-plus'
ERROR in ./node_modules/combined-stream/lib/combined_stream.js
Module not found: Error: Can't resolve 'stream' in 'C:\Users\sorou\projects\tunrWeb\node_modules\combined-stream\lib'
ERROR in ./node_modules/delayed-stream/lib/delayed_stream.js
Module not found: Error: Can't resolve 'stream' in 'C:\Users\sorou\projects\tunrWeb\node_modules\delayed-stream\lib'
ERROR in ./node_modules/isstream/isstream.js
Module not found: Error: Can't resolve 'stream' in 'C:\Users\sorou\projects\tunrWeb\node_modules\isstream'
ERROR in ./node_modules/request/request.js
Module not found: Error: Can't resolve 'stream' in 'C:\Users\sorou\projects\tunrWeb\node_modules\request'
ERROR in ./node_modules/sshpk/lib/ed-compat.js
Module not found: Error: Can't resolve 'stream' in 'C:\Users\sorou\projects\tunrWeb\node_modules\sshpk\lib'
ERROR in ./node_modules/forever-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'C:\Users\sorou\projects\tunrWeb\node_modules\forever-agent'
ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'C:\Users\sorou\projects\tunrWeb\node_modules\tunnel-agent'
ERROR in ./node_modules/request/request.js
Module not found: Error: Can't resolve 'zlib' in 'C:\Users\sorou\projects\tunrWeb\node_modules\request'

This is my package.json:

{
  "name": "tunr-web",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.0.7",
    "@angular/cdk": "github:angular/cdk-builds",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/core": "^6.0.3",
    "@angular/forms": "^6.0.3",
    "@angular/http": "^6.0.3",
    "@angular/material": "github:angular/material2-builds",
    "@angular/platform-browser": "^6.0.3",
    "@angular/platform-browser-dynamic": "^6.0.3",
    "@angular/router": "^6.0.3",
    "angular-svg-round-progressbar": "^2.0.0",
    "angularfire2": "^5.0.0-rc.11",
    "core-js": "^2.5.4",
    "firebase": "^5.1.0",
    "hammerjs": "^2.0.8",
    "jquery": "^3.3.1",
    "ng-scrollreveal": "^2.2.0",
    "ng2-scroll-to-el": "^1.2.1",
    "ngx-facebook": "^2.4.0",
    "ngx-infinite-scroll": "^6.0.1",
    "ngx-sharebuttons": "^4.1.4",
    "rxjs": "^6.2.1",
    "rxjs-compat": "^6.2.1",
    "time-ago-pipe": "^1.3.2",
    "youtube-search": "^1.1.1",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.6.8",
    "@angular/cli": "~6.0.8",
    "@angular/compiler-cli": "^6.0.3",
    "@angular/language-service": "^6.0.3",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "@types/scrollreveal": "0.0.3",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "~2.7.2"
  }
}

Does somebody know what is going wrong? None of the modules mentioned above exist in my node_modules file (I can install some of them, but 'crypto', for example, is built in now). How can I get those folders? It's driving me nuts.

Denis Tsoi
  • 9,428
  • 8
  • 37
  • 56
Zizazorro
  • 1,321
  • 2
  • 11
  • 19
  • I have the same error trying to install the latest tensorflow.js module, I have deleted node_modules and reinstall them again and is not working – George C. Jul 05 '18 at 19:32
  • 1
    The weird thing is, when I paste the node_modules from my previous back up, every thing works, but when I install the node_modules via npm i, I get those errors – Zizazorro Jul 06 '18 at 08:51
  • @Zizazorro how did you solve it? I added `"browser" { ... }` into package.json, but it doesn't help me. – Junaid Jun 27 '20 at 12:02
  • @Junaid Azkar Moulana's fix worked for me, but it is indeed not reproducible. – Zizazorro Jul 09 '20 at 12:59
  • @Junaid At least in Angular 7 it only works when removing the `"baseUrl": "./",` from `src/tsconfig.app.json` – Sandro Nov 06 '20 at 23:13

11 Answers11

101

This is caused by dependencies importing node modules that are not available in the browser. Add to your package.json the modules that give errors:

"browser": {
    "http": false,
    "https":false,
    "net": false,
    "path": false,
    "stream": false,
    "tls": false
}
snorberhuis
  • 3,108
  • 2
  • 22
  • 29
  • 22
    it is working form me, but I just want to know that why we need to add as false.? – Codebrekers Apr 10 '19 at 06:31
  • I actually don't know. I was just sharing how I solved it. – snorberhuis Oct 22 '20 at 12:19
  • 2
    @Codebrekers You need to add `false` because those libraries should not be available in the browser. If you would actually need them you would need to use dependencies like `browserify-zlib`. – Sandro Nov 06 '20 at 23:09
  • 1
    At least in Angular 7 it only works when removing the `"baseUrl": "./",` from `src/tsconfig.app.json` – Sandro Nov 06 '20 at 23:11
  • 1
    How does it work in angular 10? This doesnt work for me – Sachini Witharana Nov 24 '20 at 07:06
  • 2
    The reason this happens is because when your run ng serve, it bundles up the dependencies you have in your `package.json` to serve it on the browser. However, `package.json` generally includes both server and browser dependencies, and any native module dependency (like crypto) cannot be bundled to serve on the browser. Setting `'crypto': false` in the browser section of `package.json` tells ng serve not to attempt to bundle that file. [Ref](https://stackoverflow.com/a/54162447/3411787) – Mohammad Zaid Pathan Mar 13 '21 at 10:41
  • in angular 12, add `"browser": { "crypto": false, "stream": false, "http": false, "https":false, "net": false, "path": false, "tls": false, "zlib": false }` to the browser object above – AWE FRANCIS OLAWUMI Aug 31 '22 at 12:14
14

You are using the latest version of Angular CLI. Some npm packages are no longer supported. It's now a built-in Node module. If you've depended on crypto, you should switch to the one that's built-in.

To fix your issue with crypto, stream libs, go to,

node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js

file and do the following change,

`node: {crypto: true, stream: true}`
Azkar Moulana
  • 854
  • 1
  • 8
  • 14
9

You can user patch file solve this problem.

patch.js

const fs = require('fs');
const f = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js';

fs.readFile(f, 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  var result = data.replace(/node: false/g, 'node: {crypto: true, stream: true}');

  fs.writeFile(f, result, 'utf8', function (err) {
    if (err) return console.log(err);
  });
});

package.json

"scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "node patch.js && ng build",
    "build-prod": "node patch.js && ng build --configuration=production",
    "build-staging": "node patch.js && ng build --configuration=staging"
}

make sure to "build" the code before ng serve only for the first time.

Kirill Matrosov
  • 5,564
  • 4
  • 28
  • 39
MAlinda
  • 111
  • 1
  • 4
7

I just ran into this issue as someone new to Angular, and all these other answers are actually giving you workarounds for something that shouldn't be worked around (in most cases). Actually, you need to step back and consider that you are doing something the framework does not want you to do.

In my case, what happened was, I added a dependency on a library for accessing an external API service and I tried to import it in an Angular "service". I'm still new to Angular and am coming from a C# WCF background, so in my head, a service is a server-side process. However, nothing in Angular is server-side! It is a strictly client-side framework that runs in the browser.

The way to fix this for me was to realise that my Angular service needs to strictly communicate with my own back-end, not outside websites, which meant an API that I was going to need to write myself, separately. In my case, I'm going with a MEAN stack, so this means creating an Express.js API that will communicate with the external API for me on the back-end. This has added advantages, such as being able to cache session data and other data from the external API in a Mongo database, instead of needing a fresh client-side API session every time, which would quickly exceed the daily sessions this site allows of 7500 a day, assuming I got a lot of users.

TL;DR the fix is to remove any imports to NPM packages not intended for front-end work, that require such packages like https, crypto and fs. The fs is a particular red flag. I think this implies 'file system', which your front-end certainly shouldn't have access to directly.

pipedreambomb
  • 3,711
  • 1
  • 22
  • 18
  • I've run into the similar issue recently while trying to install `google-auth-library` into my angular project. An answer here pointed out that the library is for NodeJS at the backend but not for frontend: https://stackoverflow.com/a/66585095/2185783. – maximus Jun 14 '21 at 00:17
5

If the none of the above helped, I just lost 4 hours diagnosing why an Angular app that compiled yesterday now wouldn't compile due to "Module Not Found" errors.

The problem was that yesterday when I imported a class in one of my components, WebStorm pointed to an identically-named class in my backend node app like this:

import { ClassName } from '../../../../../../../../my-node-server-app/class-name';

It was then trying to compile all of the Node-specific modules that aren't compatible with an ES2015 browser app. If you're having the same problem and none of the above helps, check your imports! Hope I saved someone's sanity out there.

Brian Burton
  • 3,648
  • 16
  • 29
  • 3
    THIS resolved my issue. Somehow importing 'Renderer' was creating 'Module not found' errors. Once I removed from my *.ts file, compiled successfully. Lost about an hour of trying to figure this out. – Eric Carr Oct 29 '20 at 05:20
  • 1
    THANKS MAN!!!, I have wasted more than 4 Hours, I had the same issue I have mistakenly imported response from "express" and that was creating issues. – coderman401 Dec 05 '22 at 07:36
4

Use ngx-build-plus package - https://github.com/manfredsteyer/ngx-build-plus - and provide the additional configuration. This way you don't have to manually update the node modules files everytime you do an install.

Jebe
  • 81
  • 4
3

I was calling redirecting my view like this: this.router.navigate(['/dashboard']) and i created a variable router of type Router in my constructor and imported from 'express' insted of '@angular/router' and I got the same problem. I removed the wrong importation and it went away

2

Worked for me through edit the file tsconfig.json (tsconfig.base.json) with:

 ...
 "baseUrl": ".",
 "paths": {
  ...
  "crypto": [
        "node_modules/crypto-js"
      ],
      "http": [
        "node_modules/stream-http"
      ],
      "https": [
        "node_modules/https-browserify"
      ]
 }

Enjoy.

1

You have to find the node packages added as dependencies which is causing this issue. Open package-lock.json, search for crypto can find the chain of dependencies leading to the package name included in dependencies section of package.json. In my case, most of these libraries were brought in with npm package 'request'.

Sanju Varghese
  • 487
  • 2
  • 4
0

The current version of Angular-cli doesn't install some packages like zlib which the older versions did. You may have to install some packages manually to resolve these errors.

Harini P
  • 629
  • 5
  • 12
0

Like said in this comment https://stackoverflow.com/a/52367183/16245434, only add after devDependecies in package.json :

"browser": {
    "crypto": false
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
revl94
  • 1