简体   繁体   English

未找到模块:错误:无法解析“加密”和无法解析“fs”

[英]Module not found: Error: Can't resolve 'crypto' and Cant resolve 'fs'

ive just started to learn react and Ive been trying to get my react app to connect to my database with我刚开始学习反应,我一直试图让我的反应应用程序连接到我的数据库

var mysql = require('mysql');
var con = mysql.createConnection({
  host: "localhost",
  user: "*",
  password: "*",
  database: "media_app"
});
con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

but when i run the app with npm start it throws a bunch of errors但是当我使用 npm start 运行应用程序时,它会引发一堆错误

Compiled with problems:X ERROR in ./node_modules/mysql/lib/Connection.js 1:13-30 Module not found: Error: Can't resolve 'crypto' in 'C:\xampp\htdocs\socialapp\node_modules\mysql\lib' BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.编译时出现问题:./node_modules/mysql/lib/Connection.js 中的 X 错误 1:13-30 未找到模块:错误:无法解析 'C:\xampp\htdocs\socialapp\node_modules\mysql 中的 'crypto' \lib' 重大更改:webpack < 5 用于默认包含 node.js 核心模块的 polyfill。 This is no longer the case.这已不再是这种情况。 Verify if you need this module and configure a polyfill for it.验证你是否需要这个模块并为它配置一个 polyfill。 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 } ERROR in ./node_modules/mysql/lib/protocol/Auth.js 3:13-30 Module not found: Error: Can't resolve 'crypto' in 'C:\xampp\htdocs\socialapp\node_modules\mysql\lib\protocol' BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.如果你想包含一个 polyfill,你需要: - 添加一个后备 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }' - 安装 'crypto-browserify' 如果你不想要包含一个 polyfill,您可以使用这样的空模块:resolve.fallback: { "crypto": false } ./node_modules/mysql/lib/protocol/Auth.js 中的错误 3:13-30 找不到模块:错误:无法解析 'C:\xampp\htdocs\socialapp\node_modules\mysql\lib\protocol' 中的 'crypto' 重大更改:webpack < 5 用于默认包含 node.js 核心模块的 polyfill。 This is no longer the case.这已不再是这种情况。 Verify if you need this module and configure a polyfill for it.验证你是否需要这个模块并为它配置一个 polyfill。 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 } ERROR in ./node_modules/mysql/lib/protocol/sequences/Query.js 3:9-22 Module not found: Error: Can't resolve 'fs' in 'C:\xampp\htdocs\socialapp\node_modules\mysql\lib\protocol\sequences'如果你想包含一个 polyfill,你需要: - 添加一个后备 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }' - 安装 'crypto-browserify' 如果你不想要包含一个 polyfill,您可以使用这样的空模块:resolve.fallback: { "crypto": false } ./node_modules/mysql/lib/protocol/sequences/Query.js 中的错误 3:9-22 模块不是发现:错误:无法解析“C:\xampp\htdocs\socialapp\node_modules\mysql\lib\protocol\sequences”中的“fs”

removing the var mysql = require('mysql') fixes it but then i cant connect to the database删除var mysql = require('mysql')修复它,但我无法连接到数据库

There were 22 of these instead of 3 for other things it couldnt resolve like buffer, url, tls and other things but i just ran npm install ... for each one and the errors went away but these ones didnt when i ran npm install crypto or fs and i have tried to add其中有 22 个而不是 3 个用于其他无法解决的问题,例如缓冲区、url、tls 和其他东西,但我只是运行 npm install ... 对于每一个错误都消失了,但是当我运行 npm install crypto 时这些错误没有或 fs,我已尝试添加

"browser": {
    "crypto": false
}

into package.json ive tried with the tsconig.json file and ive tried adding resolve.fallback into the webpack config file but nothing seems to work进入 package.json 我尝试使用 tsconig.json 文件并尝试将 resolve.fallback 添加到 webpack 配置文件中,但似乎没有任何效果

First off, check all your import statements within your own files.首先,检查您自己文件中的所有导入语句。 I was using express on the backend and vscode intellisense auto-imported: import { response } from 'express' which caused an error with the 'fs' module.我在后端使用 express 并且 vscode intellisense auto-imported: import { response } from 'express'导致 'fs' 模块出错。

You will also need to polyfill your code.您还需要polyfill您的代码。 As you can see in your error message, there is a breaking change as Webpack version 5 does not polyfill core node.js modules by default.正如您在错误消息中看到的那样,由于 Webpack 版本 5 默认情况下不填充核心 node.js 模块,因此发生了重大变化。 For example, 'crypto' is a core node.js module that is no longer polyfilled by default.例如,'crypto' 是一个核心 node.js 模块,默认情况下不再填充。

This is how I resolved the polyfilling issue with npx-create-react-app and webpack5, hopefully it will work for you.这就是我用 npx-create-react-app 和 webpack5 解决 polyfilling 问题的方法,希望它对你有用。

You will need to npm i crypto-browserify (as mentioned in your error message).您将需要npm i crypto-browserify (如您的错误消息中所述)。

Then locate the webpack.config.js file at: yourapp/node_modules/react-scripts/config/webpack.config.js然后在以下位置找到 webpack.config.js 文件:yourapp/node_modules/react-scripts/config/webpack.config.js

Look through the webpack.config.js file and find (you can ctrl + f and type 'resolve') the resolve block within the modules.exports block.查看 webpack.config.js 文件并在 modules.exports 块中找到(您可以 ctrl + f 并键入“resolve”)resolve 块。 Here you need to provide a fallback for each module which cannot be found.在这里,您需要为每个找不到的模块提供后备。 This is the code that I used:这是我使用的代码:

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

Restart your project after modifying the webpack.config.js file.修改 webpack.config.js 文件后重启你的项目。

If you get subsequent errors which are stating that a polyfill is necessary, just repeat the above process for that specific module.如果您收到后续错误,说明需要使用 polyfill,只需针对该特定模块重复上述过程。 For example, if I had an error message for 'stream', I would npm i stream-browserify and modify the webpack.config.js to:例如,如果我有一条关于“stream”的错误消息,我会npm i stream-browserify browserify 并将 webpack.config.js 修改为:

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

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

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