Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dynamic Import Error Firebase Cloud Functions Typescript

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading