使用这个简单的节点获取查询时如何解决 ERR_REQUIRE_ESM 错误?

How can I resolve ERR_REQUIRE_ESM error when using this simple node-fetch query?

我正在尝试使用 node-fetch 进行简单的 API 提取。

出现以下错误:

internal/modules/cjs/loader.js:1089
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\raben\OneDrive\Work\PowerBI Embedded\Fetch_PBI_Access_Token\node_modules\node-fetch\src\index.js
require() of ES modules is not supported.
require() of C:\Users\raben\OneDrive\Work\PowerBI Embedded\Fetch_PBI_Access_Token\node_modules\node-fetch\src\index.js from C:\Users\raben\OneDrive\Work\PowerBI Embedded\Fetch_PBI_Access_Token\index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename C:\Users\raben\OneDrive\Work\PowerBI Embedded\Fetch_PBI_Access_Token\node_modules\node-fetch\src\index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\raben\OneDrive\Work\PowerBI Embedded\Fetch_PBI_Access_Token\node_modules\node-fetch\package.json.

←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:937:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:778:12)←[39m
←[90m    at Module.require (internal/modules/cjs/loader.js:961:19)←[39m
←[90m    at require (internal/modules/cjs/helpers.js:92:18)←[39m
    at Object.<anonymous> (C:\Users\raben\OneDrive\Work\PowerBI Embedded\Fetch_PBI_Access_Token\index.js:2:15)
←[90m    at Module._compile (internal/modules/cjs/loader.js:1072:14)←[39m
←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:937:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:778:12)←[39m {
  code: ←[32m'ERR_REQUIRE_ESM'←[39m
}

我的节点版本是v14.17.6

我已经安装了 node-fetch

这是我的 index.js:

const Fetch = require('node-fetch')

fetch("https://api.github.com/users")
    .then((res) => res.json())
    .then((res) => console.log(res));

这是我的 package.json:

{
  "name": "Fetch_PBI_Access_Token",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "node-fetch": "^3.0.0"
  }
}

如果我添加“type”:“module”,我会收到以下错误:

 This file is being treated as an ES module because it has a '.js' file extension and 'C:\Users\raben\OneDrive\Work\PowerBI Embedded\Fetch_PBI_Access_Token\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
        at file:///C:/Users/raben/OneDrive/Work/PowerBI%20Embedded/Fetch_PBI_Access_Token/index.js:2:15
    ←[90m    at ModuleJob.run (internal/modules/esm/module_job.js:170:25)←[39m
    ←[90m    at async Loader.import (internal/modules/esm/loader.js:178:24)←[39m
    ←[90m    at async Object.loadESM (internal/process/esm_loader.js:68:5)←[39m

错误真的说明了一切。由于 node-fetch 是一个 ES 模块,你不应该使用 require 语法来导入它,而是 import 语法:

import fetch from 'node-fetch';

注:
旧版本的 node-fetch 仍然是 CommonJS 包(即,可以与 require 一起使用),所以如果你将你的依赖降级到某个 2.x 版本,你的代码应该按原样工作。