const fetch = require("node-fetch"); ^ 错误 [ERR_REQUIRE_ESM]:ES 模块的 require()

Blo*_*stY 6 node.js node-fetch

我有一个问题,我对编程一无所知,我想制作一个 nft 集合,我正在关注这个 YouTube 视频:https://www.youtube.com/watch ?v=AaCgydeMu64

\n

一切都很顺利,直到 (32:14) 左右\n我的文本与视频中的文本相同,所以我不明白发生了什么。\n当我运行命令时:node utils/nftport/uploadFile.js

\n

它说:

\n
const fetch = require("node-fetch");\n              ^\n\nError [ERR_REQUIRE_ESM]: require() of ES Module ......\\hashlips_art_engine-main\\node_modules\\node-fetch\\src\\index.js from ......\\hashlips_art_engine-main\\utils\\nftport\\uploadFile.js not supported.\nInstead change the require of index.js in ......\\hashlips_art_engine-main\\utils\\nftport\\uploadFile.js to a dynamic import() which is available in all CommonJS modules.\n    at Object.<anonymous> ......\\hashlips_art_engine-main\\utils\\nftport\\uploadFile.js:2:15) {\n  code: ?[32m\'ERR_REQUIRE_ESM\'?[39m\n
Run Code Online (Sandbox Code Playgroud)\n

注意!:(......) 只是对应该存在的文件的写入替换

\n

这是 uploadFile.js 的代码:

\n
const FormData = require("form-data");\nconst fetch = require("node-fetch");\nconst basePath = process.cwd();\nconst fs = require("fs");\n\nfs.readdirSync(`${basePath}/build/images`).forEach((file) => {\n    const formData = new FormData();\n    const fileStream = fs.createReadStream(`${basePath}/build/images/${file}`);\n     formData.append("file", fileStream);\n\n    let url = "\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89";\n\nlet options = {\n  method: \'POST\',\n  headers: {\n    Authorization: \'\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\',\n  },\n  body: formData\n};\n\nfetch(url, options)\n  .then(res => res.json())\n  .then(json => {\n      const fileName = path.parse(json.file_name).name;\n      let rawdata = fs.readFileSync(`${basePath}/build/json/${fileName}.json`);\n      let metaData = JSON.parse(rawdata);\n\n      metaData.file_url = json.ipfs_url;\n\n      fs.writeFileSync(`${basePath}/build/json/${fileName}.json`,JSON.stringify(metaData, null, 2));\n\n      console.log(`${json.file_name} upload & ${fileName}.jsonupdated!`);\n  })\n  .catch(err => console.error(\'error:\' + err));\n
Run Code Online (Sandbox Code Playgroud)\n

我一直被困在这里,我觉得我已经尝试了一切,但我对编程一无所知,所以我发现这真的很难!我尝试过不同版本的 Node(我认为)我已经在互联网上阅读了所有解决方案,但没有任何效果!请帮我解释一下,以便我理解。谢谢!

\n

jfr*_*d00 9

首先,nodejs 支持两种不同的模块类型 - 用于require()加载其他模块的原始 CommonJS 模块和用于加载其他模块的较新的 ECMAScript 模块(ESM)import

您的视频教程使用的是较旧的 CommonJS 模块类型,但最新版本node-fetch仅支持由 ESM 模块加载,而这不是您所拥有的。因此,不兼容会导致您看到错误。

我知道你有三个选择:

  1. 您可以安装旧版本的node-fetch,它应该与您的视频教程完全兼容并使用require().

  2. 您可以将代码切换到 ESM 模块。为此,您必须使用适当的import语法加载所有模块(您的教程不会向您展示)。由于 CommonJS 是 Nodejs 中的默认模块类型,因此切换到 ESM 模块需要将文件扩展名更改为.mjs或创建 package.json 文件并设置type: "module"为项目的特征。

  3. node-fetch您可以安装并加载模块,而不是尝试加载node-fetch-commonjs。它是一个 CommonJS 包装器node-fetch,应该为您提供兼容性。

  • 选项#3 很有效,谢谢! (3认同)
  • @BloomstY - 请在 github 存储库中阅读[此](https://github.com/codeSTACKr/video-source-code-create-nft-collection#es-module-error-err_require_esm)。它告诉您如何使他们的代码与“node-fetch”一起工作。它就在您的确切错误的说明中。并且,说明是如何加载旧的 v2 版本的 node-fetch:“npm uninstall node-fetch”,然后“npm install node-fetch@2”。 (2认同)