im trying to make a flutter app that uses flutter cloud functions as the backend. Whenever I try to npm run serve I get this issue… has anyone seen this before? Im new to cloud functions but it doesnt make sense to me to use the dynamic import… thanks!
✔ functions: Using node@18 from host.
⬢ functions: Failed to load function definition from source: FirebaseError: Failed to load function definition from source: Failed to generate manifest from function source: Error [ERR_REQUIRE_ESM]: require() of ES Module myapp/functions/node_modules/chatgpt/build/index.js from myapp/functions/lib/index.js not supported.
Instead change the require of myapp/node_modules/chatgpt/build/index.js in myapp/functions/lib/index.js to a dynamic import() which is available in all CommonJS modules.
➜ functions node --version
v18.15.0
➜ functions firebase --version
11.25.1
{
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017",
"esModuleInterop": true,
},
"compileOnSave": true,
"include": [
"src"
]
}
>Solution :
It seems like Firebase Cloud Functions uses CommonJS modules, while chatgpt library you are importing uses ECMAScript modules.
In order to fix it change your code from:
const chatgpt = require('chatgpt');
to:
const chatgpt = await import('chatgpt');