#env varis

9 messages · Page 1 of 1 (latest)

coarse needle
#

how I can tell convex in a monorepo, that it should set the VITE env variables?
before it did it perfectly because it was beside my actual app but after moving it out to the packages/backend it somehow lost the context

fluid sundialBOT
#

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):

  1. Run npx convex dev from the frontend app folder
    From apps/web, run:

    npx convex dev
    

    This lets Convex detect Vite and write VITE_CONVEX_URL into apps/web/.env.local as described in the docs. [React URL config]

  2. Set VITE_CONVEX_URL yourself 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
#

@coarse needle is this the same logic for the delpoy command?

#

@fluid sundial is this the same logic for the deploy command?

fluid sundialBOT
#

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 (or CONVEX_DEPLOYMENT if no deploy key). [CLI deploy]
  • When you pass --cmd 'npm run build', Convex sets a single URL env var (e.g. CONVEX_URL or 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:
    npx convex deploy --cmd 'npm run build' --cmd-url-env-var-name VITE_CONVEX_URL
    
    so you don’t have to rely on auto-detection. [CLI deploy; Custom hosting]

For npx convex dev in local dev:

  • It writes the dev deployment URL into .env.local / .env and 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:

  • dev infers the env var name and writes to .env.local in the project where you run it.
  • deploy sets 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.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: