#node environment
4 messages · Page 1 of 1 (latest)
Hi @wild tide. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Yes, I can help you with that. There are indeed multiple discussions about organizing code between files that need a Node.js environment and files that can run in the default Convex environment. Here's a summary of the relevant information from the knowledge sources:
-
The main way to use Node.js in Convex is by adding the
"use node"directive at the top of a file. This is explained in the Functions > Runtimes > Node.js runtime (https://docs.convex.dev/functions/runtimes#nodejs-runtime) documentation. Files with this directive can only contain action functions and cannot include other Convex functions like queries or mutations. -
In a discussion about file organization, a community member suggested using a naming convention like
<file>.tsfor regular Convex functions and<file>Node.tsfor files that need the Node.js environment. This approach is mentioned in the Discord support thread: Use Node only for specific actions (https://discord.com/channels/1019350475847499849/1247234873589104783). -
Another thread discusses the organization of files within the
convexdirectory. It suggests grouping functions by logical entities and using the_prefix for folders or files to exclude them from Convex function auto-generation. This is detailed in the Discord support thread: File based routing: How to organize files? (https://discord.com/channels/1019350475847499849/1240118627156492288). -
When using Node.js for specific actions, it's recommended to keep the Node.js code separate from regular Convex functions. You can call Node.js actions from other Convex functions using
ctx.runAction(). This is explained in the Functions > Actions > Calling third-party APIs and using NPM packages (https://docs.convex.dev/functions/actions#calling-third-party-apis-and-using-npm-packages) documentation.