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
- How to Avoid Double Requests in function...
I'm currently using Appwrite's `functions.createExecution` in my project. I want to avoid double requests when multiple actions (like searching or pagination) a...
- Project in AppWrite Cloud doesn't allow ...
I have a collection where the data can't be opened. When I check the functions, there are three instances of a function still running that can't be deleted. The...
- Get team fail in appwrite function
I try to get team of a user inside appwrite function, but i get this error: `AppwriteException: User (role: guests) missing scope (teams.read)` If i try on cl...