【问题标题】:How to tell node my script is not a module如何告诉节点我的脚本不是模块
【发布时间】:2021-11-11 23:13:03
【问题描述】:

如何告诉节点我的脚本不是模块? 显然,如果 node_modules 中的任何内容是模块节点,则会认为我的脚本是一个模块并失败:Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:

【问题讨论】:

标签: node.js package.json


【解决方案1】:

正如您提到的,您使用的是 node-fetch 和节点 v14.16.1。内部node-fetch 文档

node-fetch 是一个 ESM-only 模块 - 你不能用 require 导入它。我们建议您继续使用使用 CommonJS 构建的 v2,除非您自己使用 ESM。我们将继续为它发布重要的错误修复。

所以你有两个选择:

  • 继续使用node-fetch 3,并更改为import fetch from 'node-fetch';。您可能需要将脚本更改为 .mjs
  • 降级到node-fetch 2 才能使用require

【讨论】:

  • 嗯。我想我不会再使用 node-fetch 了。
猜你喜欢
  • 1970-01-01
  • 2011-05-22
  • 2018-07-23
  • 2011-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-06
  • 1970-01-01
相关资源
最近更新 更多