Tondi
Tondi7d ago

Running "npx convex deploy --cmd 'next build'" command fails the build

Help me. I want to deploy to prod. I'm using vercel. I did the configuration like in the docs by setting CONVEX_DEPLOY_KEY env var only for prod. But now getting this error in logs: ✖ Vercel build environment detected but CONVEX_DEPLOY_KEY is not set. Set this environment variable to deploy from this environment. See https://docs.convex.dev/production/hosting Error: Command "npx convex deploy --cmd 'next build'" exited with 1. And if I set an empty string for previews/development environment for CONVEX_DEPLOY_KEY, I get this: ✖ Detected a non-production build environment and "CONVEX_DEPLOY_KEY" for a production Convex deployment. I'm stuck.
Hosting and Deployment | Convex Developer Hub
Share your Convex backend and web app with the world.
14 Replies
Convex Bot
Convex Bot7d 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!
ballingt
ballingt7d ago
@Tondi When you say you set CONVEX_DEPLOY_KEY env var, how did you do that? The error message sounds like it's not there.
Tondi
TondiOP7d ago
setting it in vercel like the docs say: https://docs.convex.dev/production/hosting/vercel
Using Convex with Vercel | Convex Developer Hub
Host your frontend on Vercel and your backend on Convex.
ballingt
ballingt7d ago
And when/where do you see that error?
Tondi
TondiOP7d ago
all the commits I pushed after changing the build command in vercel, have failed build. Here is the latest build logs attached.
Tondi
TondiOP7d ago
The above one is for when I set CONVEX_DEPLOY_KEY both for prod and dev
ballingt
ballingt7d ago
This doesn't look like a prod, why are you setting it for dev? What build command are you using for dev? I think there's something wrong or confusing about the docs here, just digging in to figure out The intention of the docs is - do a deploy for prod (that's what deploys your site) - if you want (and you're on a pro plan, required in order to spin up lots of preview deploys), do a preview deploy for previews. If you don't, then you use your prod convex backend for that instead of deploying. What is dev, is that for running tests? reading, dev is just when you run locally This error is saying "Don't deploy your dev functions to prod!" presumably you don't want to update your production deployment with new convex functions every time you push to dev I suggest following the docs and - only setting CONVEX_DEPLOY_KEY in prod, and only running a deploy in prod - not setting CONVEX_DEPLOY_KEY in dev, that means on your local machine. On your local machine, use npm run dev instead of a deploy command. It looks like your latest build is a preview deploy, right? You can either use convex preview deployments, or use a different build command for previews where you don't deploy the convex functions.
Tondi
TondiOP7d ago
dev, i mean staging. In vercel platform,there can only be one build command per project for both prod and staging. It's set to "npx convex deploy --cmd 'next build'" when I started getting the build failures. I thought like the docs says, when CONVEX_DEPLOY_KEY is set only for prod, It updates the production deployment. For staging build where CONVEX_DEPLOY_KEY is not set, I get a different error message:
Installing dependencies...
Lockfile is up to date, resolution step is skipped
Already up to date
Done in 2.5s
Detected Next.js version: 15.1.4
Running "npx convex deploy --cmd 'next build'"
✖ Vercel build environment detected but CONVEX_DEPLOY_KEY is not set. Set this environment variable to deploy from this environment. See https://docs.convex.dev/production/hosting
Error: Command "npx convex deploy --cmd 'next build'" exited with 1
Installing dependencies...
Lockfile is up to date, resolution step is skipped
Already up to date
Done in 2.5s
Detected Next.js version: 15.1.4
Running "npx convex deploy --cmd 'next build'"
✖ Vercel build environment detected but CONVEX_DEPLOY_KEY is not set. Set this environment variable to deploy from this environment. See https://docs.convex.dev/production/hosting
Error: Command "npx convex deploy --cmd 'next build'" exited with 1
ballingt
ballingt7d ago
I think by staging you mean a preview deployment?
Tondi
TondiOP7d ago
Yes, for vercel.
ballingt
ballingt7d ago
Got it. You have two choices: you can use a convex pro plan to use preview Convex deployments, or you can use a different build command for preview deployments vs prod. Using a different command in preview deployments vs production looks like this:
if [ "$VERCEL_ENV" = "preview" ]; then npm run build; else npx convex deploy --cmd 'next build'; fi
if [ "$VERCEL_ENV" = "preview" ]; then npm run build; else npx convex deploy --cmd 'next build'; fi
Interesting that this isn't well-documented, I'll add this to the docs You'll want to to connect to either your dev convex deployment or your prod convex deployment to power this staging (preview) deployment. These both have downsides. Connecting to prod means if you made changes to convex functions, they might not match up. Connecting to your dev deployment will work at first, but if you change branches and keep developing that preview deployment may stop working correctly This is similar to other databases, you have to decide if you want your preview deployments talking to prod or not
Tondi
TondiOP7d ago
I thought that "npx convex deploy --cmd ..." checks that already. if there is CONVEX_DEPLOY_KEY set, deploy to prod, if not just run npm build. Currently I'm the only developer working on the project, I don't think I need Pro plan or Previews. I think I can do with the 2 prod and dev convex dbs: work with dev until a feature is ready then push to prod. I just want the right command for preview deployments vs production to be handled.
ballingt
ballingt7d ago
I thought that "npx convex deploy --cmd ..." checks that already.
Intersting, does it say that somewhere? I don't think it does that, I think you need to use a different deploy command in order to do something different in preview deployments vs prod
Tondi
TondiOP7d ago
I don't know, I just thought that it works that way, given that vercel has only one build command. But thanks for the shell script, I think it unblocked me.

Did you find this page helpful?