#env varis
9 messages · Page 1 of 1 (latest)
Hi @coarse needle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Convex decides which env var name to write (e.g. VITE_CONVEX_URL vs NEXT_PUBLIC_CONVEX_URL) by trying to detect the frontend framework in the directory where you run npx convex dev. It only writes to the .env.local / .env in that project, and those values are only for the frontend client; backend functions never read from them. [React URL config]
After moving Convex into packages/backend, if you now run npx convex dev from there, Convex no longer “sees” your Vite app, so it doesn’t know to write VITE_CONVEX_URL into the app’s .env.local.
In a monorepo like:
apps/
web/ # Vite app
packages/
backend/ # convex/
you have two practical options (both are consistent with the docs):
-
Run
npx convex devfrom the frontend app folder
Fromapps/web, run:npx convex devThis lets Convex detect Vite and write
VITE_CONVEX_URLintoapps/web/.env.localas described in the docs. [React URL config] -
Set
VITE_CONVEX_URLyourself in the app-
Get the deployment URL from the Convex dashboard (Deployment Settings). [React URL config]
-
In your Vite app, create or edit
apps/web/.env.local:VITE_CONVEX_URL="<https://your-deployment.convex.cloud"> -
Use it in your client:
const convex = new ConvexReactClient(import.meta.env.VITE_CONVEX_URL as string);
-
There’s nothing in the knowledge sources about a CLI flag to “force VITE mode” from a backend-only folder, so if you want Convex to auto-manage VITE_CONVEX_URL, you need to run npx convex dev in the Vite app directory; otherwise, manage that env var manually.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@coarse needle Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@coarse needle is this the same logic for the delpoy command?
@fluid sundial is this the same logic for the deploy command?
Let me check the knowledge base for you again. One moment! :books:
Not exactly the same, and the behavior for deploy is better documented than for dev.
For npx convex deploy in CI / production:
- The target deployment is chosen from
CONVEX_DEPLOY_KEY(orCONVEX_DEPLOYMENTif no deploy key). [CLI deploy] - When you pass
--cmd 'npm run build', Convex sets a single URL env var (e.g.CONVEX_URLor a framework-specific one) for that build command, and your frontend build reads it. [CLI deploy; Vercel how it works] - You can explicitly control the name with
--cmd-url-env-var-name:so you don’t have to rely on auto-detection. [CLI deploy; Custom hosting]npx convex deploy --cmd 'npm run build' --cmd-url-env-var-name VITE_CONVEX_URL
For npx convex dev in local dev:
- It writes the dev deployment URL into
.env.local/.envand tries to infer the right variable name (VITE_CONVEX_URL,NEXT_PUBLIC_CONVEX_URL, etc.) based on the frontend framework in the directory where you run it. [React URL config; Project configuration]
So:
devinfers the env var name and writes to.env.localin the project where you run it.deploysets an env var for the build command, and you can force the name with--cmd-url-env-var-name, which is the recommended way in monorepos.