Squirble
Squirble4w ago

`tsc -b` fails because @convex/_generated references server side code

This is my project: https://github.com/nickretallack/ranked-choice-convex When I run npm run build it runs tsc -b and gives the attached errors. The logic seems to all be that a server-side file is referenced by another server side file that is referenced by the generated API that the frontend uses. What should I do to make typescript happy?
5 Replies
Convex Bot
Convex Bot4w ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
ian
ian4w ago
Does the /tsconfig.app.json file only include src (and therefore not convex/)? You could compare your setup against one of the templates generated with npx create convex@latest to bisect / debug where the issue might be
Squirble
SquirbleOP4w ago
Perhaps the real question is, how do I share code between the client and server? That's where the issue is. Or perhaps it's that the generated API file seems to be referencing files that maybe it shouldn't? I'm not sure what I'm supposed to do. Like, I don't see any reason why the generated API should mention userHelpers, which is a private server side thing that isn't even a convex mutation or anything. It's just a regular helper function that the client doesn't need to know about. in fact, it seems like all four of the files in question are just server side helpers that shouldn't be exposed as part of the API at all, yet there they are in that generated file
sshader
sshader4w ago
Like, I don't see any reason why the generated API should mention userHelpers
Current behavior for Convex generated code is to import any TS file in the convex/ directory in _generated/api, regardless of whether that file contains convex functions itself or not. This is mostly to make the code generation simpler (i.e., only require listing files in the directory vs. actually evaluating all of them to check for convex functions), and could potentially change someday. But if you have code that you want to share between client and server, I generally put that in its own directory (e.g. common or shared) and import that from both the convex/ code and my frontend code (and it'll probably require you to add common to your tsconfig.app.json) . I'm not super familiar with the tsconfig setup here, but I think it's generally easier to manage things if the only imports from convex from the frontend code is the _generated/api file
Squirble
SquirbleOP4w ago
oh, I was told that I should put the shared code inside the convex directory. Does convex still work if it references code outside its directory? I guess it must, since it can use node_modules...

Did you find this page helpful?