Best Ways to solve Error: “Module not found: Error: Can’t resolve ‘crypto’”

JavaScript needs no introduction as it is a worldwide accepted programming language to make various projects for websites and applications to involve the users with user-friendly interactive programs. The ability to have wonderful designs make the beginners learn this language to step into the shoe of professional programmers. When working on JavaScript, you may face many errors, including syntax errors, nameErrors, typeErrors, Not Found errors, and many other types of errors. You may also encounter the Error: “Module not found: Error: Can’t resolve ‘crypto’”, which may be new for you if you are a beginner.

There is nothing wrong with getting yourself into the error, in fact, it is completely normal. Today, we provide the best ways to solve the error warning. First, have a look at how the error shows up

How error warning pops up

When trying to run ng serve, you may have an error warning.

Let’s say, you have a JSON package:

{   "name": "ProName",   "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-devkit/build-angular": "~0.12.0",
    "@angular/animations": "5.2.10",
    "@angular/common": "5.2.10",
    "@angular/compiler": "5.2.10",
    "@angular/compiler-cli": "5.2.10",
    "@angular/core": "5.2.10",
    "@angular/forms": "5.2.10",
    "@angular/platform-browser": "5.2.10",
    "@angular/platform-browser-dynamic": "5.2.10",
    "@angular/router": "5.2.10",
    "@types/dotenv": "^4.0.3",
    "@types/errorhandler": "0.0.32",
    "@types/express": "^4.16.0",
    "@types/node": "^10.5.1",
    "apostille-library": "^7.1.0",
    "core-js": "^2.5.4",
    "dotenv": "^6.0.0",
    "errorhandler": "^1.5.0",
    "express": "^4.16.0",
    "nem2-sdk": "^0.9.7",
    "rxjs": "~6.3.3",
    "stream": "0.0.2",
    "tslib": "^1.9.0",
    "typescript": "^2.9.2",
    "zone.js": "~0.8.26"   } }

You end up with the error warning

ERROR in ./node_modules/aws-sign2/index.js Module not found: Error: Can't resolve 'crypto' in '/Users/MYPC/Documents/Myproj/ProName/node_modules/aws-sign2' 

ERROR in ./node_modules/aws4/aws4.js Module not found: Error: Can't resolve 'crypto' in '/Users/MYPC/Documents/Myproj/ProName/node_modules/aws4' ERROR in ./node_modules/ecc-jsbn/index.js

Now that you know how you land up in the error warning, let’s figure out how to fix it

Solutions to Handle Error: “Module not found: Error: Can’t resolve ‘crypto’”

Solution 1 – Addition in package.json

Adding a code in the package.json proves to be beneficial when solving the error. The code you need to add under devDependency is given below

"devDependencies": {

    ...

},

"browser": {

    "crypto": false

}

Solution 2 – The tsconfig.ts setting

To fix the error warning, you need to do the tsconfig.ts setting.  For this, use the following code

"compilerOptions": {

"baseUrl": "./",

"paths": {

  "crypto": [

    "../../node_modules/crypto-js"

  ]

}

Solution 3 – The ‘Crypto’: false setup

In the case, you are not willing to use the crypto, then you can set it to false to any dependencies that are shown. Use the below to solve the error

"browser": {

        "crypto": false,

        "

Solution 4 – Change the crypto-js version

Sometimes, the error only shows up because of the crypto version you are using. You can try the following version so that you can get rid of the error

npm install [email protected]

Solution 5 – The webpack config file set up

To remove the error, you need to add the following code

resolve: { 
    fallback: { crypto: false }, 
},

Conclusion

And that exactly how you can fix Error: “Module not found: Error: Can’t resolve ‘crypto’”. It is completely on you to choose any of the solutions that suit your project. I hope you find it helpful!

Reference Source: https://www.folkstalk.com/2022/07/module-not-found-error-cant-resolve-crypto-react-with-code-examples.html

https://www.thecodeteacher.com/question/71594/javascript—Module-not-found:-Error:-Can’t-resolve-‘crypto’

Leave a Reply

Your email address will not be published. Required fields are marked *