小编Mas*_*aya的帖子

如何将节点获取与 Cloud Functions for Firebase 结合使用。无法部署

我想通过 Flutter 调用 Cloud Functions 获取数据。

这是我的代码。

const functions = require("firebase-functions");
const admin = require('firebase-admin');
const fetch = require('node-fetch');
admin.initializeApp();

exports.postalCode = functions.https.onCall( (data, context) => {
    const URL = 'https://zipcloud.ibsnet.co.jp/api/search?zipcode=100-0002';
    return fetch(URL).then(res => res.json());
});
Run Code Online (Sandbox Code Playgroud)

错误代码

错误 [ERR_REQUIRE_ESM]:必须使用 import 加载 ES 模块:/Users/xxx/node_modules/node-fetch/src/index.js

不支持 ES 模块的 require()。/Users/xxx/xxx/functions/index.js 中的 /Users/xxx/node_modules/node-fetch/src/index.js 的 require() 是一个 ES 模块文件,因为它是一个距离父包最近的 .js 文件。 package.json 包含 "type": "module" ,它将该包范围内的所有 .js 文件定义为 ES 模块。

相反,将 /Users/xxx/node_modules/node-fetch/src/index.js 重命名为以 .cjs 结尾,更改所需代码以使用 import(),或从 /Users/xxx/node_modules 中删除 "type": "module" /node-fetch/package.json。

at Object.Module._extensions..js (internal/modules/cjs/loader.js:1102:13)
at …
Run Code Online (Sandbox Code Playgroud)

node.js flutter google-cloud-functions node-fetch

8
推荐指数
1
解决办法
3073
查看次数