For some reason appwrite is not able to access the node-appwrite module
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)
What's the code?
How do you have imported appwrite?
Are you sure it's imported correctly?
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);
}
};
did you add build command??
No, is the build command needed ?? Cause have used the same template in other projects & it was working
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.
oh okay I will try that
Facing the same err 😭
from the error you posted, a build command should fix it. is there any other error? could you tell what steps did you take?
Hey you can have a look at the source code above. This is the package.json
{
"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"
}
}
& yeah I just deployed it through github in appwrite
Open you Function on Console > Settings > Build Settings > Click.
Does it show npm install?
It was not there, I just added it rn
aah i see, you added build in package.json but it needs to be added on console for the function.
Oh okay my bad 😂
cool. just redeploy from the console and it should work.
Recommended threads
- IP / CIDR For Appwrite Cloud Functions
[I saw this message](https://discord.com/channels/564160730845151244/1330335737954046067) that went unanswered. I have the same question: what IP addresses / CI...
- Cloudflare output appearing in function ...
Hello everyone! My function execution failed and it showing Cloudflare output in the execution log. It leaks the IP (DigitalOcean) as well. It's not the first ...
- How to access oldValue payload in functi...
I want to record old and new values of data changes as audits. But when i try to read row in function. The value is new already even before grt updated