最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error when attempting to include Crypto module: Can't resolve 'crypto' - Stack Overflow

programmeradmin0浏览0评论

I am working on a register user setup in React JS, installed crypto library from to secure passwords but when I run the program , it gives me an error :

ERROR in ./node_modules/jwa/index.js 5:13-30

Module not found: Error: Can't resolve 'crypto' in 'E:\Node Tutorial\registerSetup\client\node_modules\jwa'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default. This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:

  • add a fallback 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }'
  • install 'crypto-browserify' If you don't want to include a polyfill, you can use an empty module like this: resolve.fallback: { "crypto": false }

Here is my package.json file :

         {
          "name": "registerSetup",
          "version": "1.0.0",
          "description": "",
          "main": "index.js",
          "scripts": {
          "start": "NODE_ENV= production node server",
          "dev": "nodemon server"
          },
          "keywords": [],
          "author": "",
          "license": "ISC",
          "dependencies": {
          "body-parser": "^1.19.1",
          "cors": "^2.8.5",
          "crypto": "^1.0.1",
          "crypto-browserify": "^3.12.0",
          "dotenv": "^15.0.0",
          "express": "^4.17.2",
          "express-jwt": "^6.1.0",
          "express-validator": "^6.14.0",
          "google-auth-library": "^7.11.0",
          "jsonwebtoken": "^8.5.1",
          "loadash": "^1.0.0",
          "lodash": "^4.17.21",
          "mongoose": "^6.1.8",
          "morgan": "^1.10.0",
          "node-fetch": "^3.2.0",
          "nodemailer": "^6.7.2",
          "nodemon": "^2.0.15"
          }
         }

The package.json for my react folder in registerSetup -->

I am working on a register user setup in React JS, installed crypto library from to secure passwords but when I run the program , it gives me an error :

ERROR in ./node_modules/jwa/index.js 5:13-30

Module not found: Error: Can't resolve 'crypto' in 'E:\Node Tutorial\registerSetup\client\node_modules\jwa'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default. This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:

  • add a fallback 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }'
  • install 'crypto-browserify' If you don't want to include a polyfill, you can use an empty module like this: resolve.fallback: { "crypto": false }

Here is my package.json file :

         {
          "name": "registerSetup",
          "version": "1.0.0",
          "description": "",
          "main": "index.js",
          "scripts": {
          "start": "NODE_ENV= production node server",
          "dev": "nodemon server"
          },
          "keywords": [],
          "author": "",
          "license": "ISC",
          "dependencies": {
          "body-parser": "^1.19.1",
          "cors": "^2.8.5",
          "crypto": "^1.0.1",
          "crypto-browserify": "^3.12.0",
          "dotenv": "^15.0.0",
          "express": "^4.17.2",
          "express-jwt": "^6.1.0",
          "express-validator": "^6.14.0",
          "google-auth-library": "^7.11.0",
          "jsonwebtoken": "^8.5.1",
          "loadash": "^1.0.0",
          "lodash": "^4.17.21",
          "mongoose": "^6.1.8",
          "morgan": "^1.10.0",
          "node-fetch": "^3.2.0",
          "nodemailer": "^6.7.2",
          "nodemon": "^2.0.15"
          }
         }

The package.json for my react folder in registerSetup --> https://pastebin./hqBs7J4s

Share Improve this question edited Feb 3, 2022 at 15:03 isherwood 61.1k16 gold badges121 silver badges169 bronze badges asked Feb 3, 2022 at 12:15 user17896109user17896109 4
  • have you tried putting var crypto = require("crypto") or import crypto from "node:crypto" in your code? – RedYetiDev Commented Feb 3, 2022 at 12:42
  • yes , check : pastebin./ZCxF3K7s – user17896109 Commented Feb 3, 2022 at 12:51
  • Can you tell use your version of react-scripts in package.json? – Magofoco Commented Feb 3, 2022 at 13:02
  • @Magofoco 5.0.0 react scripts version – user17896109 Commented Feb 3, 2022 at 14:03
Add a ment  | 

3 Answers 3

Reset to default 4

I think you should go to your node_modules/react-scripts/config/webpack.config.json and there write the following code in resolve block

fallback: {
           "crypto": require.resolve("crypto-browserify")
          }  

Note: crypto-browserify should be installed

Crypto is a built-in Node module you don't have to install it. Uninstall "crypto": "^1.0.1" and then try:

const crypto = require("crypto")

You can try to downgrade the react-scripts in package.json version to : 4.0.3

发布评论

评论列表(0)

  1. 暂无评论