Skip to content
Back

[SOLVED] Using fetch inside Appwrite function

  • 0
  • Self Hosted
  • Functions
loup
29 Jul, 2023, 16:15

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 :

TypeScript
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...

TL;DR
User is facing an issue using fetch with `node-fetch` inside a NodeJS Appwrite function. They receive an error related to ES modules. The issue is resolved by downgrading the version of node-fetch to `"node-fetch": "^2.6.12"`. The user has initialized their function correctly using the Appwrite CLI.
Drake
29 Jul, 2023, 16:31

How did you initialize your function?

loup
29 Jul, 2023, 17:21

There is my function :

TypeScript
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

Drake
29 Jul, 2023, 17:45

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

loup
29 Jul, 2023, 19:08

yes i did that

Drake
29 Jul, 2023, 19:31

What version of node?

Drake
29 Jul, 2023, 19:32

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

Drake
29 Jul, 2023, 19:35

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

loup
29 Jul, 2023, 20:20

[SOLVED] Usgin fetch inside Appwrite function

loup
29 Jul, 2023, 20:20

[SOLVED] Using fetch inside Appwrite function

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more