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.jsduring 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
- TablesDB can't be used in Appwrite Funct...
I have written a function (DART) and it won't deploy. Here is what I get : 2026-03-14T17:09:41.459693680Z Compiling ... 2026-03-14T17:09:42.915619217Z ../build...
- Realtime for files() works almost well, ...
I have been trying to make use of realtime, today (14.03.26) I have pulled all the latest versions of docker images, and sdk available. Whats working: - Conn...
- Local appwrite run functions --user-id n...
Hi, I'm running into an issue when testing Appwrite functions locally with user impersonation. I'm using a self-hosted Appwrite instance and running functions ...