1

I'm quite the beginner when it comes to developing web/node.js applications (so apologies if I am not explaining myself well enough) and am now working on an angular and angular material project. I think this is beside the point however...

My application is written in typescript - however I am using a typescript library that was compiled to javascript. This library uses "web-request". So far I am only using one file from this library (but this file uses others from the same library) which I will name "file_1.js" by doing import { MyClass } from '../../lib/file_1' and myclass = new MyClass();.

file_1.d.ts and file_1.js.map exist as well and I can only assume they are implemented properly. In fact, I have copy-pasted this library from a react project that I worked on a while back and it worked just fine there.

file_1.js only uses this library in two instances: therefore has:

const WebRequest = require("web-request");
let response = yield WebRequest.get(openUri, requestOptions);
let response = yield WebRequest.post(postUri, requestOptions, jsonBody);

I am very confident the library itself is sound. When trying to run my server via ng serve I get a ton of issues with dependencies of web-request not being resolved:

Error: ./node_modules/aws-sign2/index.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/aws-sign2'

Error: ./node_modules/aws4/aws4.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/aws4'

Error: ./node_modules/ecc-jsbn/index.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/ecc-jsbn'

Error: ./node_modules/http-signature/lib/signer.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/http-signature/lib'

Error: ./node_modules/http-signature/lib/verify.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/http-signature/lib'

Error: ./node_modules/oauth-sign/index.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/oauth-sign'

Error: ./node_modules/request/lib/oauth.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/request/lib'

Error: ./node_modules/request/lib/helpers.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/request/lib'

Error: ./node_modules/request/lib/hawk.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/request/lib'

Error: ./node_modules/sshpk/lib/identity.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib'

Error: ./node_modules/sshpk/lib/fingerprint.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib'

Error: ./node_modules/sshpk/lib/utils.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib'

Error: ./node_modules/sshpk/lib/signature.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib'

Error: ./node_modules/sshpk/lib/private-key.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib'

Error: ./node_modules/sshpk/lib/certificate.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib'

Error: ./node_modules/sshpk/lib/key.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib'

Error: ./node_modules/sshpk/lib/dhe.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib'

Error: ./node_modules/sshpk/lib/formats/pem.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib/formats'

Error: ./node_modules/sshpk/lib/formats/ssh-private.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib/formats'

Error: ./node_modules/sshpk/lib/formats/openssh-cert.js
Module not found: Error: Can't resolve 'crypto' in '/home/ubuntu/my_website/node_modules/sshpk/lib/formats'

Error: ./node_modules/request/lib/har.js
Module not found: Error: Can't resolve 'fs' in '/home/ubuntu/my_website/node_modules/request/lib'

Error: ./node_modules/request/request.js
Module not found: Error: Can't resolve 'zlib' in '/home/ubuntu/my_website/node_modules/request'

I tried applying the solution suggested by snorberhuis here: Angular 6 many Can't resolve errors (crypto, fs, http, https, net, path, stream, tls, zlib) by adding this to my package.json:

  "browser": {
    "http": false,
    "https":false,
    "net": false,
    "path": false,
    "stream": false,
    "tls": false,
    "crypto": false,
    "fs": false,
    "zlib": false
  }

The server starts with no issues then - but the browser window remains blank white with an error in the console:

enter image description here

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
user46794
  • 51
  • 6

1 Answers1

1

The problem is that the 'web-request' module is not written to work in the browser rather on the node server. You need to use a different package for what you need to achieve.

kvetis
  • 6,682
  • 1
  • 28
  • 48
  • 1
    In fact you shouldn't need any package at all because browsers nowadays natively support [`fetch`](https://developer.mozilla.org/docs/Web/API/Fetch_API) – CherryDT Mar 30 '21 at 12:47
  • 1
    Yes, and in angular, you can use HttpClient for that. – kvetis Mar 30 '21 at 13:07