【问题标题】:Transpiling node-fetch require returns error转译 node-fetch 要求返回错误
【发布时间】:2021-12-25 03:53:40
【问题描述】:

我遇到了node-fetch 库的问题。 新版本 (3.0.3) 在其 package.json 中有 "type": "module"。这是一个问题,因为我将代码从 ES6 转换为使用 require 的常规 js,然后我得到 ...firebase_datasource.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. 我可以使用早期版本的node-fetch,它的package.json 中的package.json 中不包含"type": "module",但这不是解决方案。 处理这个问题的正确方法是什么?

【问题讨论】:

    标签: ecmascript-6 transpiler node-fetch


    【解决方案1】:

    node-fetch's README 解释该怎么做:

    CommonJS

    v3 中的node-fetch 是一个仅限 ESM 的模块 - 您无法使用 require() 导入它。

    如果您无法切换到 ESM,请使用与 CommonJS 保持兼容的 v2。将继续为 v2 发布关键错误修复。

    npm install node-fetch@2
    

    或者,您可以使用 CommonJS 中的 async import() 函数来异步加载 node-fetch

    // mod.cjs
    const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
    

    【讨论】:

    • 谢谢,完全错过了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 2019-08-16
    • 2017-10-07
    • 2020-11-06
    • 2016-09-11
    • 2020-05-25
    • 1970-01-01
    相关资源
    最近更新 更多