Module not found: Error: Can't resolve 'crypto' Module not found: Error: Can't resolve 'crypto' javascript javascript

Module not found: Error: Can't resolve 'crypto'


I ran into a similar issue lately while trying to use another library (tiff.js) in a small project I was experimenting with.

The way I got around this was to add the following to my package.json file, right after the devDependencies section.

"devDependencies": {    ...},"browser": {    "crypto": false}

This didn't seem to have any adverse effect when trying to use the library in the application.


Adding this setting in tsconfig.json file under that project resolve this warning

"compilerOptions": {"baseUrl": "./","paths": {  "crypto": [    "node_modules/crypto-js"  ]}


I like R. Richards's answer, but I thought it would be useful to provide some more information.

This is a known issue with Angular, and the Angular CLI dev team seems to think it's a feature rather than a bug. I, as well as other developers in this issue thread, disagree. Contributors to that thread provided several workaround fixes, but my project didn't compile successfully until I implemented R. Richards' solution. I didn't revert the previous changes, though, so tacnoman's and GrandSchtroumpf's fixes may be of use to others.

Some, like clovis1122 here and others in that issue thread, have questioned why a web app would need access to these libraries and why the necessary tasks can't be completed on the server side instead. I can't speak for everyone, but my use case is that, when authenticating a user account, Strapi responds with a JSON Web Token string that must be decoded by the client. Since the necessary library depends on crypto and stream, you won't be able to extract the JWT expiration time unless those dependencies are available.

In case anyone has trouble extrapolating from R. Richards' answer, you'll have to set to false any dependencies that are showing up in "can't resolve x" errors. For example, the critical part of my package.json is:

    "browser": {        "crypto": false,        "stream": false    }