简体   繁体   English

Webpack 无法解析 Node.js 模块

[英]Webpack cannot resolve Node.js modules

Webpack cannot resolve core Node.js modules Webpack 无法解析核心 Node.js 模块

I'm aware that this isn't the only Node.js module query related to Webpack on here, but when applying the solutions that I've read elsewhere, yet another core module seems to be causing another ReferenceError.我知道这不是与此处的 Webpack 相关的唯一 Node.js 模块查询,但是当应用我在其他地方读过的解决方案时,另一个核心模块似乎正在导致另一个 ReferenceError。

I have an index.js file that requires the Crypto module to log a hash to the console:我有一个 index.js 文件,它需要Crypto 模块将 hash 记录到控制台:

index.js index.js

const crypto = require('crypto')

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
                   .update('I love cupcakes')
                   .digest('hex');
console.log(hash);

When setting up my webpack.congif.js file, I got back this error:在设置我的webpack.congif.js文件时,我得到了这个错误:

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。 If you want to include a polyfill, you need to:如果你想包含一个 polyfill,你需要:

  • add a fallback 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }'添加一个后备 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }'
  • install 'crypto-browserify'安装“加密浏览器”

After following the above guidance, I got the same error for 'buffer' and 'stream' modules, and to get the config file to compile, I've added fallback statements and installed the relevant modules to cover these missing dependencies:遵循上述指导后,“buffer”和“stream”模块出现相同的错误,为了编译配置文件,我添加了回退语句并安装了相关模块以覆盖这些缺失的依赖项:

webpack.config.js webpack.config.js

const path = require('path');

module.exports = {
    mode: 'development',
    node: {
        global: true,
    },
    resolve: {
        fallback: { 
            "crypto": require.resolve("crypto-browserify"),
            "buffer": require.resolve("buffer/"),
            "stream": require.resolve("stream-browserify")
        }
    },
    entry: './src/index.js',
    output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist'),
    },
};

Webpack.config now compiles, but when I run index.html in the browser I get the following error in the browser console: Webpack.config现在可以编译,但是当我在浏览器中运行index.html时,我在浏览器控制台中收到以下错误:

Uncaught ReferenceError: process is not defined未捕获的 ReferenceError:未定义进程

As stated in the 'Breaking Change' error, Webpack 5 no longer polyfills for node core modules, but does this mean that a polyfill is required for every single node module before you can run a node script?如“重大更改”错误中所述,Webpack 5 不再为节点核心模块填充,但这是否意味着在运行节点脚本之前每个节点模块都需要填充?

Try with process/browser polyfill.尝试使用process/browser polyfill。 Put this into your webpack.config.js :将其放入您的webpack.config.js

resolve: {
  alias: {
    process: "process/browser"
  },
  ...
}

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

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