带有 node-fetch 的 Webpack 配置给出了“node:buffer”不由插件处理

Nag*_*tna 7 node.js reactjs webpack node-fetch

我正在尝试使用 React 应用程序和 Webpack 设置来实现节点获取。但在使用节点获取时。我收到大量错误。

    ERROR in node:buffer

Module build failed: UnhandledSchemeError: Reading from "node:buffer" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:832:25
    at Hook.eval [as callAsync] (eval at create (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Object.processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:829:8)
    at processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:220:11)
    at iteratePitchingLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:171:10)
    at runLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:398:2)
    at NormalModule._doBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:819:3)
    at NormalModule.build (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:963:15)
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/Compilation.js:1371:12
    at NormalModule.needBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:1256:26)


ERROR in node:fs

Module build failed: UnhandledSchemeError: Reading from "node:fs" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:832:25
    at Hook.eval [as callAsync] (eval at create (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Object.processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:829:8)
    at processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:220:11)
    at iteratePitchingLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:171:10)
    at runLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:398:2)
    at NormalModule._doBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:819:3)
    at NormalModule.build (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:963:15)
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/Compilation.js:1371:12
    at NormalModule.needBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:1256:26)


ERROR in node:https

Module build failed: UnhandledSchemeError: Reading from "node:https" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:832:25
    at Hook.eval [as callAsync] (eval at create (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Object.processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:829:8)
    at processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:220:11)
    at iteratePitchingLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:171:10)
    at runLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:398:2)
    at NormalModule._doBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:819:3)
    at NormalModule.build (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:963:15)
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/Compilation.js:1371:12
    at NormalModule.needBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:1256:26)


ERROR in node:http

Module build failed: UnhandledSchemeError: Reading from "node:http" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:832:25
    at Hook.eval [as callAsync] (eval at create (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/tapable/lib/Hook.js:18:14)
    at Object.processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:829:8)
    at processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:220:11)
    at iteratePitchingLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:171:10)
    at runLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:398:2)
    at NormalModule._doBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:819:3)
    at NormalModule.build (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:963:15)
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/Compilation.js:1371:12


ERROR in node:net

Module build failed: UnhandledSchemeError: Reading from "node:net" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:832:25
    at Hook.eval [as callAsync] (eval at create (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Object.processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:829:8)
    at processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:220:11)
    at iteratePitchingLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:171:10)
    at runLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:398:2)
    at NormalModule._doBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:819:3)
    at NormalModule.build (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:963:15)
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/Compilation.js:1371:12
    at NormalModule.needBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:1256:26)


ERROR in node:path

Module build failed: UnhandledSchemeError: Reading from "node:path" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:832:25
    at Hook.eval [as callAsync] (eval at create (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Object.processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:829:8)
    at processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:220:11)
    at iteratePitchingLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:171:10)
    at runLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:398:2)
    at NormalModule._doBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:819:3)
    at NormalModule.build (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:963:15)
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/Compilation.js:1371:12
    at NormalModule.needBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:1256:26)


ERROR in node:process

Module build failed: UnhandledSchemeError: Reading from "node:process" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:832:25
    at Hook.eval [as callAsync] (eval at create (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Object.processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:829:8)
    at processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:220:11)
    at iteratePitchingLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:171:10)
    at runLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:398:2)
    at NormalModule._doBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:819:3)
    at NormalModule.build (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:963:15)
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/Compilation.js:1371:12
    at NormalModule.needBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:1256:26)


ERROR in node:stream/web

Module build failed: UnhandledSchemeError: Reading from "node:stream/web" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:832:25
    at Hook.eval [as callAsync] (eval at create (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Object.processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:829:8)
    at processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:220:11)
    at iteratePitchingLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:171:10)
    at runLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:398:2)
    at NormalModule._doBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:819:3)
    at NormalModule.build (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:963:15)
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/Compilation.js:1371:12
    at NormalModule.needBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:1256:26)


ERROR in node:stream

Module build failed: UnhandledSchemeError: Reading from "node:stream" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:832:25
    at Hook.eval [as callAsync] (eval at create (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Object.processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:829:8)
    at processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:220:11)
    at iteratePitchingLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:171:10)
    at runLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:398:2)
    at NormalModule._doBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:819:3)
    at NormalModule.build (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:963:15)
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/Compilation.js:1371:12
    at NormalModule.needBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:1256:26)


ERROR in node:url

Module build failed: UnhandledSchemeError: Reading from "node:url" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:832:25
    at Hook.eval [as callAsync] (eval at create (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Object.processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:829:8)
    at processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:220:11)
    at iteratePitchingLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:171:10)
    at runLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:398:2)
    at NormalModule._doBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:819:3)
    at NormalModule.build (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:963:15)
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/Compilation.js:1371:12
    at NormalModule.needBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:1256:26)


ERROR in node:util

Module build failed: UnhandledSchemeError: Reading from "node:util" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:832:25
    at Hook.eval [as callAsync] (eval at create (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Object.processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:829:8)
    at processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:220:11)
    at iteratePitchingLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:171:10)
    at runLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:398:2)
    at NormalModule._doBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:819:3)
    at NormalModule.build (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:963:15)
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/Compilation.js:1371:12
    at NormalModule.needBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:1256:26)


ERROR in node:zlib

Module build failed: UnhandledSchemeError: Reading from "node:zlib" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:832:25
    at Hook.eval [as callAsync] (eval at create (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Object.processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:829:8)
    at processResource (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:220:11)
    at iteratePitchingLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:171:10)
    at runLoaders (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/loader-runner/lib/LoaderRunner.js:398:2)
    at NormalModule._doBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:819:3)
    at NormalModule.build (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:963:15)
    at /mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/Compilation.js:1371:12
    at NormalModule.needBuild (/mnt/c/Users/nageshkatna/Desktop/frontend/node_modules/webpack/lib/NormalModule.js:1256:26)
Run Code Online (Sandbox Code Playgroud)

这就是我的 webpack 配置的样子

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
module.exports = {
  entry: path.resolve(__dirname, './src/index.tsx'),
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
  },
  module: {
    rules: [
      {
        test: /\.(ts|js)x?$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader',
          },
        ],
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader', 'postcss-loader'],
      },
      {
        test: /\.(?:ico|gif|png|jpg|jpeg)$/i,
        type: 'asset/resource',
      },
      {
        test: /\.(woff(2)?|eot|ttf|otf|svg|)$/,
        type: 'asset/inline',
      },
    ],
  },
  devServer: {
    hot: true,
    open: true,
  },
  output: {
    path: path.resolve(__dirname, '..', './build'),
    filename: 'bundle.js',
  },
  mode: 'development',
  devtool: 'cheap-module-source-map',
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, './src/index.html'),
    }),
    new CopyPlugin({
      patterns: [{ from: 'src', to: 'build' }],
    }),
  ],
};
Run Code Online (Sandbox Code Playgroud)

我只想在这里使用它

import fetch from 'node-fetch';

const login = (body) => {
  const env = process.env;

  fetch(`${env.AUTH_API_BASE_URL}/user/authentication`, {
    method: 'post',
    body: JSON.stringify(body),
    headers: { 'Content-type': 'application/json' },
  })
    .then((response) => {
      console.log(response);
    })
    .catch((err) => {
      const res = {
        message: err.message,
        status: err.status,
      };
      return res;
    });
};

export default login;
Run Code Online (Sandbox Code Playgroud)

我是否缺少 Webpack 上的配置?我尝试安装诸如node-polyfill-webpack-plugin和 之类的软件包node-libs-browser,但没有成功,或者可能我无法正确配置它们。