Back

Facing issues while executing cloud functions in appwrite

  • 0
  • Functions
Akash Srinivasan
27 Jun, 2024, 13:17

For some reason appwrite is not able to access the node-appwrite module

TypeScript
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'node-appwrite' imported from /usr/local/server/src/function/main.js
    at new NodeError (node:internal/errors:372:5)
    at packageResolve (node:internal/modules/esm/resolve:954:9)
    at moduleResolve (node:internal/modules/esm/resolve:1003:20)
    at defaultResolve (node:internal/modules/esm/resolve:1218:11)
    at ESMLoader.resolve (node:internal/modules/esm/loader:580:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:294:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:80:40)
    at link (node:internal/modules/esm/module_job:78:36)
TL;DR
Developers are facing issues with cloud functions in Appwrite. To solve it, they need to add a build command (`npm install`) in the console settings. The error is due to the node-appwrite module not being accessed properly in the code.
D5
27 Jun, 2024, 13:36

What's the code?

D5
27 Jun, 2024, 13:36

How do you have imported appwrite?

D5
27 Jun, 2024, 13:36

Are you sure it's imported correctly?

Akash Srinivasan
27 Jun, 2024, 13:41
TypeScript
import { Client, Users, Query } from 'node-appwrite';

export default async ({ req, res, log, error }) => {
  // Initialize Appwrite client
  const client = new Client()
    .setEndpoint("https://cloud.appwrite.io/v1")
    .setProject("PROJECT")
    .setKey('API_KEY'); // Ensure you have this key set in your environment variables

  const users = new Users(client);

  if (req.method === 'POST' && req.path === '/webhook') {
    const order = req.body.data.attributes;
    log(order);

    const userEmail = order.user_email;
    if (!userEmail) {
      error('User email not found in webhook payload.');
      return res.json({ success: false }, 400);
    }

    try {
      // Search for the user by email
      const searchResult = await users.list([Query.equal("email", userEmail)]);
      if (!searchResult || searchResult.users.length === 0) {
        error(`User with email ${userEmail} not found.`);
        return res.json({ success: false }, 404);
      }

      // Update user labels
      const user = searchResult.users[0];
      const updatedLabels = [...new Set([...(user.labels || []), 'pro'])];
      await users.update(user.$id, { labels: updatedLabels });
      log(`Added "pro" label to user ${user.$id}.`);

      return res.json({ success: true });
    } catch (err) {
      error(`Failed to update user: ${err.message}`);
      return res.json({ success: false }, 500);
    }
  } else {
    return res.send('HELLO !!', 200);
  }
};
darShan
27 Jun, 2024, 13:42

did you add build command??

Akash Srinivasan
27 Jun, 2024, 13:44

No, is the build command needed ?? Cause have used the same template in other projects & it was working

darShan
27 Jun, 2024, 13:45

yupp. if you intend to add an external package, you need to add build command, usually - npm install for nodejs. Save the build settings & redeploy the function.

Akash Srinivasan
27 Jun, 2024, 13:46

oh okay I will try that

Akash Srinivasan
27 Jun, 2024, 13:54

Facing the same err 😭

darShan
27 Jun, 2024, 13:55

from the error you posted, a build command should fix it. is there any other error? could you tell what steps did you take?

Akash Srinivasan
27 Jun, 2024, 13:56

Hey you can have a look at the source code above. This is the package.json

TypeScript
{
  "name": "webhooks",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "type": "module",
  "scripts": {
    "build": "npm install"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "node-appwrite": "^13.0.0"
  }
}
Akash Srinivasan
27 Jun, 2024, 13:56

& yeah I just deployed it through github in appwrite

darShan
27 Jun, 2024, 13:57

Open you Function on Console > Settings > Build Settings > Click.

Does it show npm install?

Akash Srinivasan
27 Jun, 2024, 13:59

It was not there, I just added it rn

darShan
27 Jun, 2024, 13:59

aah i see, you added build in package.json but it needs to be added on console for the function.

Akash Srinivasan
27 Jun, 2024, 13:59

Oh okay my bad πŸ˜‚

darShan
27 Jun, 2024, 13:59

cool. just redeploy from the console and it should work.

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