[英]Error when attempting to include Crypto module: Can't resolve 'crypto'
我正在 React JS 中進行注冊用戶設置,安裝加密庫以保護密碼,但是當我運行程序時,它給了我一個錯誤:
/node_modules/jwa/index.js 5:13-30 中的錯誤
未找到模塊:錯誤:無法解析“E:\Node Tutorial\registerSetup\client\node_modules\jwa”中的“crypto”
重大更改:webpack < 5 用於默認包含 node.js 核心模塊的 polyfill。 這已不再是這種情況。 驗證你是否需要這個模塊並為它配置一個 polyfill。
如果你想包含一個 polyfill,你需要:
- 添加一個后備 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }'
- install 'crypto-browserify' 如果你不想包含 polyfill,你可以像這樣使用一個空模塊:resolve.fallback: { "crypto": false }
這是我的 package.json 文件:
{
"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"
}
}
我在 registerSetup 中的 react 文件夾的 package.json --> https://pastebin.com/hqBs7J4s
Crypto 是一個內置的 Node 模塊,您無需安裝它。 卸載“crypto”:“^1.0.1”,然后嘗試:
const crypto = require("crypto")
你可以嘗試將 react 版本降級到: 4.0.3
我認為您應該 go 到您的 node_modules/react-scripts/config/webpack.config.json 並在解析塊中寫入以下代碼
fallback: {
"crypto": require.resolve("crypto-browserify")
}
注意:應該安裝crypto-browserify
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.