
Probably a silly question, but Im not able to use fetch() with node-fetch
inside a NodeJS Appwrite function. I've try this :
const fetch = require("node-fetch");
But I got an error :
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /usr/code-start/node_modules/node-fetch/src/index.js
require() of ES modules is not supported.
require() of /usr/code-start/node_modules/node-fetch/src/index.js from /usr/code-start/src/index.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.
Instead rename /usr/code-start/node_modules/node-fetch/src/index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /usr/code-start/node_modules/node-fetch/package.json.
How im supposed to add fetch in my function ? Ive try to renaming index.js
to index.cjs
like the error said but appwrite dont find index.js
during execution...

How did you initialize your function?

There is my function :
const appwriteSDK = require("node-appwrite");
const fetch = require("node-fetch");
const novuSDK = require("@novu/node")
module.exports = async function (req, res) {
....
Well its working with this version of node-fetch : "node-fetch": "^2.6.12"
but not with the latest

So you didn't initialize the function using the Appwrite CLI (appwrite init function
)?

yes i did that

What version of node?

Did you change the package.json file created by the init?

Actually never mind. Downgrading is the right approach. Node-fetch v3 is incompatible

[SOLVED] Usgin fetch inside Appwrite function

[SOLVED] Using fetch inside Appwrite function
Recommended threads
- Appwrite Functions Generally Down?
We are getting customer support requests, that our service is down. We tried to pinpoint it, and we can't see any logs of any appwrite cloud function in the pas...
- Unable to migrate the project to self ho...
The Continue button is disabled in the migration modal. Unable to migrate the project. In both local and cloud appwrite 1.7.4
- Why a REST client keeps getting "The cur...
Why a REST client or API call keeps getting "The current user is not authorized to perform the requested action" even if the roles are configured correctly? Wha...
