Captain
Captain2y ago

Convex in a NX monorepo

I have a NX monorepo with 1 next.js application(for now). i came across Convex a few days ago and its looks very interesting. but i cant figure out how to use Convex in a NX monorepo. Anyone?
37 Replies
ballingt
ballingt2y ago
@Captain Are you running into any issues? Do you use package.json-based Nx monorepo or a "integrated" monorepo?
Captain
CaptainOP2y ago
integrated ideally i would run convex as a lib but im fine with application too the problem is that npx convex dev must be run at the root of the workspace which creates the convex 'files' outside apps or libs
ballingt
ballingt2y ago
Ah gotcha, that makes sense
Captain
CaptainOP2y ago
do you have any suggestions?
ballingt
ballingt2y ago
That's a requirement in an Nx repo, that everything not have its own package.json and everything be in apps or libs? If you create a file called convex.json next to the package.json you can customize the location of the convex directory.
ballingt
ballingt2y ago
Hopefully this solves the issue, you can set this to apps/convex or apps/backend/convex
Captain
CaptainOP2y ago
the convex folder at the root of the project still gets created but this folder only has 2 files now. readme and tsconfig the rest do go into the exptected location
ballingt
ballingt2y ago
So you have two convex folders? You can get rid of the top level one and just use the one in the apps or libs folders
Captain
CaptainOP2y ago
what about the tsconfig?
ballingt
ballingt2y ago
Is there not one in the lower convex directory? Move it down there if not
Captain
CaptainOP2y ago
no there isnt. im not sure if you are familliar with NX. but in NX each app or lib has its own tsconfig which extends a base tsconfig. i could move the file to right convex dir and merge the changes im well versed ins TS but im not as great with config and the ts compiler options. afraid this approach might have consequences in the broader NX monorepo.
ballingt
ballingt2y ago
Not familiar, no. You could extend the base tsconfig but I bet you don't need to? It sounds like we should make a demo Nx repo to show one way this could work
Captain
CaptainOP2y ago
i think that'd be very useful. especially with attracting pro dev teams
ballingt
ballingt2y ago
The two things that need to work are - convex dev should be able to typecheck (but you can turn this off), and - other projects need to be able to import from convex/_generated/api
Captain
CaptainOP2y ago
the 2nd point is where nx shines. apps can import any number of libs. 1 caveat, apps cant import other apps code. only lib code can be imported
ballingt
ballingt2y ago
If these both work already then you're all set Another approach would be sticking the convex folder inside the Next.js app — until you're trying to use your Convex API from multiple webapps that approach sounds simplest Either of these approaches shouldn't be a problem, but let us know if you hit anything. We've worked with folks using turborepo for this and we use Rush internally, curious about the Nx integrated approach. It sounds like paddling upstream when projects like Next.js expect to control their own package.json files but if you can get it to work it sounds simple and convenient.
Captain
CaptainOP2y ago
would using urborepo be easier? im not married to nx
ballingt
ballingt2y ago
If you're flexible but you want to use Nx it could be that the package.json-style monorepo is smoother — but I think what you're doing should work, especially if you put the convex directory in the Next.js project I'm trying it in a repo now, should have something soon I'm curious to hear why you're interested in Nx and why integrated over separate package.json — it's absolutely something that should work with Convex, but curious to hear more for how we document it. So far what I understand about Nx is that it is roughly like Bazel, and sounds nice for multilanguage monorepos
Captain
CaptainOP2y ago
if i put the convex folder inside the next.js app dir, then other apps cant import the functions and generated code
ballingt
ballingt2y ago
Got it, and you want other apps to use the Convex API in addition to the Next.js one
Captain
CaptainOP2y ago
i have been using nx for a while. in my opinion its a great builder. however, right now, i have more use for the realtimeness of convex. using nx is absolutely not a must. i would however like to use a monorepo setup.
ballingt
ballingt2y ago
Here's a model to follow for using Convex in a monorepo, here using turborepo: https://github.com/allenhartwig/convex-turborepo/tree/main/apps
Captain
CaptainOP2y ago
im gonna try turborepo
ballingt
ballingt2y ago
sweet, I'll share a Nx integrated solution for future searchers when I get one working but it's taking a while
Captain
CaptainOP2y ago
i have created a solution for nx but i cant give guarantees when it comes to the tsconfig part what is needed is a node or ts(only tried node) lib. this lib exports the _generated folder of convex and all custom functions the only problem i have is that it works both with and without changing the tsconfig of the lib which makes me worry
ballingt
ballingt2y ago
Sorry late to see this, this sounds alright. You should only need to export that _generated folder. The tsconfig.json file there is only used to typecheck and to bundle these functions, and is mostly there so you can choose what setting you want to typecheck the functions with. @Captain curious if you came up with something like this https://github.com/thomasballinger/convex-nx-integrated-monorepo
Captain
CaptainOP2y ago
i see you went with the packages approach. i use the integrated approach. i will publish what i have on github tomorrow and send you the link
ballingt
ballingt2y ago
I don't think I did, there's only one package.json at the top
Captain
CaptainOP2y ago
if the tsconfig truly doesnt matter then i think a simple lib dedicated to convex(or as a data-access lib) should work
ballingt
ballingt2y ago
maybe I'm misunderstanding what the packages approach is I followed this https://nx.dev/getting-started/tutorials/integrated-repo-tutorial
Captain
CaptainOP2y ago
ohh i see. the example also uses packages in the integrated example.
Captain
CaptainOP2y ago
GitHub
GitHub - sitepoint-editors/nx-nextjs-monorepo
Contribute to sitepoint-editors/nx-nextjs-monorepo development by creating an account on GitHub.
Captain
CaptainOP2y ago
when setting up the integrated monorepo, you need to choose react as the base and next.js as the framework. this sets up 2 things. apps folder with 1 next.js app and a empty libs folders
ballingt
ballingt2y ago
Ah interesting, thanks for the pointer. Looks like it would be similar mostly, sounds like yours can be the example of that style.
Captain
CaptainOP2y ago
do convex functions only run in the browser?
Indy
Indy2y ago
Convex functions run on the backend only. You use them on the frontend by calling our specially made react hooks.

Did you find this page helpful?