javascript - 如何修复 "BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default"错误?

标签 javascript laravel vue.js webpack web3

我要使用 "web3" "walletconnect/web3-provider" vue 和 laravel 8 上的包。我通过 npm i --save web3 @walletconnect/web3-provider 命令安装它,然后添加以下代码以导入 vue。

import Vue from "vue";
import Web3 from "web3";
import WalletConnect from "@walletconnect/client";
import QRCodeModal from "@walletconnect/qrcode-modal"
import WalletConnectProvider from "@walletconnect/web3-provider";

const connector = new WalletConnect({
    bridge: "https://bridge.walletconnect.org", // Required
    qrcodeModal: QRCodeModal,
});
window.walletConnector = connector;

//  Create WalletConnect Provider
const provider = new WalletConnectProvider({
    infuraId: "27e484dcd9e3efcfd25a83a78777cdf1",
});

//  Enable session (triggers QR Code modal)
await provider.enable();
但我得到这个错误:

ERROR in ./node_modules/cipher-base/index.js 2:16-43 Module not found: Error: Can't resolve 'stream' in '/var/www/tok/node_modules/cipher-base'

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.

If you want to include a polyfill, you need to:

  • add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
  • install 'stream-browserify' If you don't want to include a polyfill, you can use an empty module like this: resolve.fallback: { "stream": false }

ERROR in ./node_modules/keccak/lib/api/keccak.js 1:22-39 Module not found: Error: Can't resolve 'stream' in '/var/www/tok/node_modules/keccak/lib/api'

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.

If you want to include a polyfill, you need to:

  • add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
  • install 'stream-browserify' If you don't want to include a polyfill, you can use an empty module like this: resolve.fallback: { "stream": false }

ERROR in ./node_modules/keccak/lib/api/shake.js 1:22-39 Module not found: Error: Can't resolve 'stream' in '/var/www/tok/node_modules/keccak/lib/api'

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.

If you want to include a polyfill, you need to:

  • add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
  • install 'stream-browserify' If you don't want to include a polyfill, you can use an empty module like this: resolve.fallback: { "stream": false }

webpack compiled with 3 errors


显然这个错误与 Webpack 配置有关。我该如何解决?有人可以帮助我吗?

最佳答案

运行npm i react-app-rewired创建 config-overrides.js 文件并添加:

const webpack = require('webpack');
module.exports = function override(config, env) {

    config.resolve.fallback = {
        url: require.resolve('url'),
        assert: require.resolve('assert'),
        crypto: require.resolve('crypto-browserify'),
        http: require.resolve('stream-http'),
        https: require.resolve('https-browserify'),
        os: require.resolve('os-browserify/browser'),
        buffer: require.resolve('buffer'),
        stream: require.resolve('stream-browserify'),
    };
    config.plugins.push(
        new webpack.ProvidePlugin({
            process: 'process/browser',
            Buffer: ['buffer', 'Buffer'],
        }),
    );

    return config;
}
从 config-overrides.js 安装所有包。
在 package.json 中,替换脚本:
"scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-app-rewired eject"
  },

关于javascript - 如何修复 "BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67348426/

相关文章:

javascript - 表单和 getElementById

css - 侧边栏菜单下拉菜单从右边下来

javascript - 使用VueJS 2.0和vue-mdl菜单组件,如何响应点击事件?

javascript - vue-echarts中的"Error: Component series.line not exists. Load it first."

javascript - nativescript-checkbox 不会显示

Javascript 反向字母表

javascript - 在 RAILS 3.1 中的 js Assets 上设置 Cache-Control header

javascript - elt.lang = 'fr' 不工作

php - Sentry 2 与 ldap

javascript - Vue.js 使用全局列表