Ajay
Ajay2w ago

Deploying Convex in a Turborepo

I've been following along the guide to deploy Convex to Vercel. Owing to the fact that my repository is a turborepo, I'm finding the deployment instructions a bit confusing. I used Better T-Stack to create my repository. This means that the Convex directory is structured as such:
todo/
├── apps
│ └── web
│ └── src
│ ├── app
│ ├── components
│ │ ├── todo
│ │ └── ui
│ ├── context
│ ├── functions
│ └── lib
├── docs
└── packages
└── backend
└── convex
└── _generated
todo/
├── apps
│ └── web
│ └── src
│ ├── app
│ ├── components
│ │ ├── todo
│ │ └── ui
│ ├── context
│ ├── functions
│ └── lib
├── docs
└── packages
└── backend
└── convex
└── _generated
Now, the guide asks the user to do: npx convex deploy --cmd 'npm run build'which I'm not sure how to manage here. Do I build these two things separately? Ideally I'd like to resolve being able to run a single build instruction (ideally the typical one in a vercel deployment). I've got the entire repo here for anyone to play around with here: https://github.com/ajay-bhargava/todo Any guidance is appreciated. I'll keep plugging away at this and report back my findings / progress if i make any.
4 Replies
Convex Bot
Convex Bot2w 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!
Ajay
AjayOP2w ago
Some related questions I have are: - In addition to the PRODUCTION_DEPLOY_KEY do I need the DEPLOY_URL? - Is this something that goes into my web application as an environment variable? - Why don't I need a deploy key for dev? If I'm understanding this:
How it works

In Vercel, we overrode the Build Command to be npx convex deploy --cmd 'npm run build'.

npx convex deploy will read CONVEX_DEPLOY_KEY from the environment and use it to set the CONVEX_URL (or similarly named) environment variable to point to your production deployment.

Your frontend framework of choice invoked by npm run build will read the CONVEX_URL (or similarly named) environment variable to point your deployed site (via ConvexReactClient) at your production deployment.

Finally, npx convex deploy will push your Convex functions to your production deployment.

Now, your production deployment has your newest functions and your app is configured to connect to it.

You can use --cmd-url-env-var-name to customize the variable name used by your frontend code if the deploy command cannot infer it, like

npx convex deploy --cmd-url-env-var-name CUSTOM_CONVEX_URL --cmd 'npm run build'
How it works

In Vercel, we overrode the Build Command to be npx convex deploy --cmd 'npm run build'.

npx convex deploy will read CONVEX_DEPLOY_KEY from the environment and use it to set the CONVEX_URL (or similarly named) environment variable to point to your production deployment.

Your frontend framework of choice invoked by npm run build will read the CONVEX_URL (or similarly named) environment variable to point your deployed site (via ConvexReactClient) at your production deployment.

Finally, npx convex deploy will push your Convex functions to your production deployment.

Now, your production deployment has your newest functions and your app is configured to connect to it.

You can use --cmd-url-env-var-name to customize the variable name used by your frontend code if the deploy command cannot infer it, like

npx convex deploy --cmd-url-env-var-name CUSTOM_CONVEX_URL --cmd 'npm run build'
Basically the CONVEX_DEPLOY_KEY is just an automatic way to set the CONVEX_URL (which I can just grab from the dashboard, right?) and then run convex deploy? Further reading ... ok, so the PRODUCTION_DEPLOY_KEY is just so that I don't have to authenticate to write the convex functions?
erquhart
erquhart2w ago
Here's a reference implementation: https://github.com/get-convex/turbo-expo-nextjs-clerk-convex-monorepo The deploy key does contain deployment info so you don't have to set the convex url, but it also serves as an admin key for the deployment, allowing functions to be uploaded from a codebase to the deployment, which happens at build time. It runs the command you provide (eg., npm run build) after Convex functions successfully deploy, and injects framework prefixed variables to the client like NEXT_PUBLIC_CONVEX_URL.
Ajay
AjayOP2w ago
Thanks! The build command:
cd ../../packages/backend && npx convex deploy --cmd 'cd ../../apps/web && turbo run build' --cmd-url-env-var-name NEXT_PUBLIC_CONVEX_URL
cd ../../packages/backend && npx convex deploy --cmd 'cd ../../apps/web && turbo run build' --cmd-url-env-var-name NEXT_PUBLIC_CONVEX_URL
Was critical. Of note it is important to replace the
../../apps/app
../../apps/app
with
../../apps/web
../../apps/web
in my specific case.

Did you find this page helpful?