节点获取:不支持的 URL 类型“节点:”:节点:缓冲区

我需要让 node-fetch 为 VUE JS 项目工作,但我遇到了这些依赖项错误:

These dependencies were not found:

* node:buffer in ./node_modules/node-fetch/src/index.js, ./node_modules/node-fetch/src/body.js
* node:http in ./node_modules/node-fetch/src/index.js, ./node_modules/node-fetch/src/headers.js
* node:https in ./node_modules/node-fetch/src/index.js
* node:net in ./node_modules/node-fetch/src/utils/referrer.js
* node:stream in ./node_modules/node-fetch/src/index.js, ./node_modules/node-fetch/src/body.js
* node:url in ./node_modules/node-fetch/src/request.js
* node:util in ./node_modules/node-fetch/src/body.js, ./node_modules/node-fetch/src/headers.js and 1 other
* node:zlib in ./node_modules/node-fetch/src/index.js

To install them, you can run: npm install --save node:buffer node:http node:https node:net node:stream node:url node:util node:zlib

我尝试 run npm install --save node:buffer node:http node:https node:net node:stream node:url node:util node:zlib 但收到此错误:

npm 错误!代码 EUNSUPPORTEDPROTOCOL npm ERR!不支持的 URL 类型“节点:”:节点:缓冲区

如何安装缺少的依赖项?

(我在 UBUNTU 18.04.6 LTS 上使用 NODE JS v16.13.2)

stack overflow node-fetch: Unsupported URL Type "node:": node:buffer
原文答案
author avatar

接受的答案

不带 (node:) 运行此命令:

npm install --save buffer http https net stream url util zlib

答案:

作者头像

我的解决方案是在 vue.config.js 文件的 configureWebpack.external 属性中包含“node-fetch”:

module.exports = {
    configureWebpack: {
        externals: {
            'node-fetch': "require('node-fetch')"
        }
    }
}

重要:您必须将 fetch-node 安装为 yarn add node-fetch@2npm install node-fetch@2 (取决于您的包管理器)