简体   繁体   English

Node Polyfills 和 webpack.config.js 的问题

[英]Issues with Node Polyfills and webpack.config.js

I'm relatively new to web development, but I'm working on a React project where I'm trying to interface with an AWS database.我是 Web 开发的新手,但我正在研究一个 React 项目,我正在尝试与 AWS 数据库进行交互。 To install MySql, I ran npm install mysql , which did end up importing the module, however I now get the error:为了安装 MySql,我运行了npm install mysql ,它最终导入了模块,但是我现在得到了错误:

Module not found: Error: Can't resolve 'crypto' in '.\node_modules\mysql\lib' 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.

I've done a lot of reading about Webpack, and I now understand that I just need to add a fallback, presumably in a new file called webpack.config.js in the root folder.我已经阅读了很多关于 Webpack 的资料,现在我明白我只需要添加一个回退,大概是在根文件夹中一个名为webpack.config.js的新文件中。 However, I made it and the error is still there.但是,我做到了,错误仍然存在。 Here's the exact code I used in the file:这是我在文件中使用的确切代码:

module.exports = 
{
    entry:"/src/out.js",

    output:{
        path : "dist",
        filename: "bundle.js"
    },

    resolve: {
        fallback: {
          "fs": false,
          "tls": false,
          "net": false,
          "path": false,
          "zlib": false,
          "http": false,
          "https": false,
          "stream": false,
          "crypto": false,
          "crypto-browserify": false,
          "timers":false
        } 
      },
}

What am I doing wrong?我究竟做错了什么? It seems like Webpack is completely ignoring the config file, is it an issue with how I wrote the config file? Webpack 似乎完全忽略了配置文件,这是我编写配置文件的方式的问题吗?

If it helps at all, I haven't changed any of the configurations bar installing the MySql module, and to export/compile I just use the npm start command, which runs it to a localhost.如果它有帮助的话,我没有更改安装 MySql 模块的任何配置栏,并且导出/编译我只是使用npm start命令,它将它运行到本地主机。

Re-add support for Node.js core modules with node-polyfill-webpack-plugin :使用node-polyfill-webpack-plugin重新添加对 Node.js 核心模块的支持:

And install dependency npm install crypto-browserify and npm install util并安装依赖npm install crypto-browserifynpm install util

With the package installed, add the following to your webpack.config.js:安装包后,将以下内容添加到您的 webpack.config.js 中:

const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")

module.exports = {
    resolve: {
      fallback: {
        // other fallbacks
         "util": require.resolve("util/"),
         "crypto": require.resolve("crypto-browserify")
      },
    }
    // Other rules...
    plugins: [
        new NodePolyfillPlugin()
    ]
}

I figured it out - for anyone with the same problem, this is what I did.我想通了 - 对于任何有同样问题的人,这就是我所做的。

When you use create-react-app to make the app, it doesn't automatically recognize webpack.config.js , so you need to run npm install react-app-rewired and create another override file called config-override.js .当您使用create-react-app制作应用程序时,它不会自动识别webpack.config.js ,因此您需要运行npm install react-app-rewired并创建另一个名为config-override.js覆盖文件。 Full instructions and files found here 完整说明和文件可在此处找到

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

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