#Convex Action Cache: Uncaught Error: Cannot invoke syscall during module imports

18 messages · Page 1 of 1 (latest)

buoyant coyote
#

Having : this error
Uncaught Error: Cannot invoke syscall during module imports
at fetch [as fetch]

when trying to use Convex Action Cache

chrome basinBOT
#

Hi @buoyant coyote. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The error message:

Uncaught Error: Cannot invoke syscall during module imports
    at fetch [as fetch]

typically occurs in Convex when you attempt to perform asynchronous operations (like calling fetch) at the top level of a module, rather than inside a function handler. In the context of using the Convex Action Cache, this can happen if you accidentally invoke code that performs network or database operations during module import, rather than inside the handler of an action.

Key points to check:

  • Make sure that any calls to fetch, database queries, or other asynchronous operations are only made inside the handler function of your action, not at the top level of your module or during the construction of the ActionCache instance.
  • The error is not specific to Action Cache, but rather to how Convex actions and modules are loaded and executed. This is confirmed by similar errors reported when invoking syscalls during module imports, not during the actual function call itself. See the discussion here: Discord support thread: call more than 1 mutation/query in an action when using "use node".

Example of what NOT to do:

// ❌ This will cause the error if fetch is called at the top level
const data = await fetch("<https://api.example.com">); // <-- BAD

Correct usage:

import { action } from "./_generated/server";
import { ActionCache } from "@convex-dev/action-cache";
import { components } from "./_generated/api";

const cache = new ActionCache(components.actionCache, {
  action: internal.example.myExpensiveAction,
});
#

export const myFunction = action({
handler: async (ctx, args): Promise<string> => {
// ✅ All async work happens inside the handler
const result = await cache.fetch(ctx, { foo: "bar" });
return result;
},
});

[Convex Action Cache documentation](<https://www.convex.dev/components/action-cache>)

**Summary:**  
Ensure that all asynchronous operations, including cache fetches and network requests, are performed inside the action handler, not at the module level. This will prevent the "Cannot invoke syscall during module imports" error. If you continue to see this error, review your imports and initialization code to ensure nothing is being executed outside of a function handler.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
#

@buoyant coyote Kapa.ai is still learning and improving, please let me know how I did by reacting below.

buoyant coyote
#

i am using action cache with use node @chrome basin can that pose a problem ?
i am using handlers

chrome basinBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

Using Action Cache with "use node" is not explicitly documented as a problem in the knowledge sources, but there are some important considerations:

  • The Action Cache component itself is designed to work with Convex actions, and actions can be run in either the default Convex runtime or the Node.js runtime (by adding "use node" at the top of the file) Components > Action Cache.
  • When using "use node", your action will run in a Node.js environment (AWS Lambda), which is necessary if you need to use Node.js-specific libraries or APIs. However, you must ensure that all asynchronous operations (like fetch or cache.fetch) are only performed inside the handler function, not at the module level, to avoid errors like "Cannot invoke syscall during module imports" Discord support thread: call more than 1 mutation/query in an action when using "use node".
  • There is no explicit mention in the knowledge sources that using Action Cache with "use node" is unsupported or problematic, as long as you follow the correct usage patterns for both Convex actions and the Action Cache.

In summary:
You can use Action Cache with "use node" as long as you ensure all async operations are inside your action handlers. If you encounter errors, double-check that nothing is being executed at the module level outside of handlers. The knowledge sources do not mention any inherent incompatibility.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@buoyant coyote Kapa.ai is still learning and improving, please let me know how I did by reacting below.

buoyant coyote
#

Ok, so i stil lhaving the same error, i am correctly using handler @chrome basin

chrome basinBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

Based on the knowledge sources, if you are still seeing the error:

Uncaught Error: Cannot invoke syscall during module imports
    at fetch [as fetch]

even though you are correctly using a handler and have "use node" at the top of your file, here are the most relevant points from the sources:

  1. This error usually means some async or syscall (like fetch, database access, etc.) is being invoked at the top level of your module, not inside the handler. Even if you think everything is inside the handler, double-check that:

    • No code outside the handler (including in class constructors, cache initializations, or imported files) is making async calls or syscalls.
    • All such operations are strictly inside the handler function of your action or inside functions called from the handler, not at the module or class level.
  2. Imported files must also follow this rule. If you import a class or function from another file, and that file does any async work or syscalls at the top level, you will get this error—even if your own file is correct. This is a common source of confusion and is mentioned in several support threads Discord support thread: call more than 1 mutation/query in an action when using "use node", Discord support thread: "use node" is present in node runtime file but still getting "you are using node APIs" error.

#
  1. "use node" must be at the top of every file in the import chain that uses Node APIs. If you import a file that uses Node APIs but does not have "use node" at the top, you will get errors Discord support thread: "use node" is present in node runtime file but still getting "you are using node APIs" error.

  2. Action Cache itself should not cause this error if used as documented. The error is not specific to Action Cache, but to how/where async or Node-specific code is executed.

If you have checked all of the above and are still seeing the error, the knowledge sources do not provide further troubleshooting steps specific to Action Cache with "use node". It may help to isolate the problem by commenting out code or imports until the error disappears, then reintroduce code piece by piece to find the culprit.

If you can share a minimal code example (with all imports), it may help to pinpoint the issue. Otherwise, based on the knowledge sources, the most likely cause is still an async or syscall at the module level, possibly in an imported file.

If you need more help, you may want to post your code and import structure in the Convex Discord for more targeted support, as suggested in the support threads.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@buoyant coyote Kapa.ai is still learning and improving, please let me know how I did by reacting below.

buoyant coyote
#

i confirm, where removing the "use node" it works fine without an issue, but with use node i get that error @chrome basin

chrome basinBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

Thank you for confirming that removing "use node" resolves the issue, but adding it causes the "Cannot invoke syscall during module imports" error.

Based on the knowledge sources, here’s what is known:

  • The "use node" directive switches your action to run in the Node.js runtime, which is required for some Node-specific libraries. However, this runtime is stricter about not allowing any syscalls (like fetch, database access, etc.) during module import—these must only occur inside the action handler itself or functions called from the handler, not at the top level or during module initialization. This is a common source of the error you’re seeing Functions > Runtimes, Discord support thread: call more than 1 mutation/query in an action when using "use node".

  • When you remove "use node", your code runs in the Convex JavaScript runtime, which is more permissive and may not trigger this error for the same code. This difference in runtime behavior is documented, and it’s expected that some code works in one runtime but not the other Functions > Actions.

  • The error is not specific to Action Cache, but rather to how the Node.js runtime in Convex handles module imports and syscalls. If any imported file, or any code outside your handler, performs a syscall, you will get this error—even if the same code works in the default runtime Discord support thread: "use node" is present in node runtime file but still getting "you are using node APIs" error.

#

Summary:
This behavior is expected: the Node.js runtime in Convex is stricter about syscalls during module import. You must ensure that all async operations and syscalls are only performed inside the handler function, and that all imported files also follow this rule. If you are certain your code follows this, but the error persists, the knowledge sources do not provide further troubleshooting steps specific to this scenario.

If you need more help, sharing a minimal reproducible example in the Convex Discord is recommended, as suggested in the support threads.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: