Mas*_*aya 8 node.js flutter google-cloud-functions node-fetch
我想通过 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 Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:14)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:92:18)
at Object.<anonymous> (/Users/nagayamasaru/Togashop/functions/index.js:3:15)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:14)
Run Code Online (Sandbox Code Playgroud)
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "14"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^9.8.0",
"firebase-functions": "^3.14.1",
"node-fetch": "file:node_modules/node-fetch"
},
"devDependencies": {
"eslint": "^7.6.0",
"eslint-config-google": "^0.14.0",
"firebase-functions-test": "^0.2.0"
},
"private": true
}
Run Code Online (Sandbox Code Playgroud)
小智 8
您应该使用node-fetch@2.6.1,因为node-fetch仅支持ESM最新版本。
相关:而是将 index.js 的 require 更改为所有 CommonJS 模块中都可用的动态 import()
| 归档时间: |
|
| 查看次数: |
3073 次 |
| 最近记录: |