简体   繁体   English

找不到模块:错误:无法解析“加密”

[英]Module not found: Error: Can't resolve 'crypto' in

Error Message:错误信息:

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.重大变化:webpack < 5 用于默认包含 node.js 核心模块的 polyfill。 This is no longer the case.这已不再是这种情况。 Verify if you need this module and configure a polyfill for it.验证您是否需要此模块并为其配置 polyfill。

So I was googling this issue and had a LOT of problems finding a solution.所以我在谷歌上搜索这个问题并且在寻找解决方案时遇到了很多问题。 So i would like to share it with you:所以我想和你分享:

  1. First of all this can happen not only with crypto but other stuff like http, https, os and so on.首先,这不仅会发生在加密货币上,还会发生在 http、https、os 等其他东西上。

  2. Check if the packet (this case crypto-browserify is installed) There should be a folder node_modules\\crypto-browserify检查数据包(这种情况下安装了crypto-browserify)应该有一个文件夹node_modules\\crypto-browserify

If it doesnt exist: npm install crypto browsrify , then yarn add @types/node@15.12.5 -D (for this node version)如果它不存在: npm install crypto browsrify ,然后yarn add @types/node@15.12.5 -D (对于这个节点版本)

In node_modules\\crypto-browserify edit package.json and add在 node_modules\\crypto-browserify 编辑 package.json 并添加

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

(after devDependencies) (在 devDependencies 之后)

  1. under tsconfig.json add在 tsconfig.json 下添加
"compilerOptions": {
"paths":{
   "crypto":["node_modules/crypto-browserify"],
   "http":["node_modules/stream-http"],
   "https":["node_modules/https-browserify"]
   },
  1. under angluar.json add在angluar.json下添加
"architect": {
   "build": {
   "builder": "@angular-devkit/build-angular:browser",
   "options": {
      "allowedCommonJsDependencies": ["crypto"],
      "allowedCommonJsDependencies": ["http"],
      "allowedCommonJsDependencies": ["https"],

I had this problem in ReactJS with create-react-app(facebook)我在 ReactJS 中使用 create-react-app(facebook) 遇到了这个问题

Solution:解决方案:

  1. First install the necessary packages "crypto-browserify"首先安装必要的包“crypto-browserify”

  2. Modify webpack.config.js in reactjs with create-react-app this file is inside:使用 create-react-app 修改 reactjs 中的 webpack.config.js 这个文件在里面:

node_modules/react-scripts/config/webpack.config.js node_modules/react-scripts/config/webpack.config.js

  • Search module.exports and inside this function there is a return:搜索 module.exports 并在此函数中返回:
module.exports = function (webpackEnv) {
  ...
  return {
   ...
    resolve: {
      ...
      fallback: {
        // Here paste
        crypto: require.resolve("crypto-browserify"),

      }
    }
  }
}

Note:Is possible that need more packages, for example "stream-browserify" the steps are same.注意:可能需要更多的包,例如“stream-browserify”步骤是一样的。 This solution works, but when the webpack project starts it shows warnings此解决方案有效,但当 webpack 项目启动时显示警告

Pd: I am not native speaker English, but I hope understand me. Pd:我的母语不是英语,但我希望能理解我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM