Which process.env does Convex expose? I
Which process.env does Convex expose? I would like to check if the functions are running in dev, preview or production.
13 Replies
I could just add a CONVEX_ENV var to production and to Default Environment Variables (preview), but if there is one already I'd prefer to use that.
Environment Variables | Convex Developer Hub
Store and access environment variables in Convex
Got this in dev!
Not sure what it will yield in production and preview though.
We've shied away from this because in many of our previous experiences this is a code smell, leading to code that behaves differently in prod.
You can always do this yourself though. I would recommend explicitly setting correct fine grained env variables. E.g. different urls for a third party you don't want to hit in previews.
The 'process.env.NODE_ENV' thing is unfortunately something of a bug.
I am setting up an internal action to send an email. The email contains a link that I want to point to the correct URL for the app.
I think our packing script added that. But @Tom knows that best.
Yep. I'd setup an explicit env variable for that.
Hmm. I am a bit screwed on previews either way. I need to make the link point to the Vercel preview deployment for things to work...
I don't think I can know which Vercel preview my current Convex preview is for...
Oh I see. I wonder if vercel gives you that env variable that you could pass to convex? @sshader has thought a bit deeper about that.
I do get the Vercel Preview URL from Vercel, but how do I get that into Convex runtime without passing it from the client I wonder...
Re
process.env.NODE_ENV
, yep this is currently 'development in all environments because it's bundled in that way by esbuild as Indy says
@Mikael Lirbank this is a reasonable thing to want! Today you'd need to write that envar to a file in your convex directory at build time like
echo "export const deployPreviewUrl='$VERCEL_DEPLOY_PREVIEW_WHATEVER'" > convex/url.ts`
this would be a first step in your build command, we can think about something cleanerI guess I could set up a Vercel function, which in turn calls Convex. Then I could pass the current app URL to Convex from Vercel. But it's a bit of a detour...
where
url.ts
has that variable normally set to localhost:3000
or whateverAh! Smart! I'll try that
OK, let me play with this for a bit. Thanks!
Hey, that works like a charm @Tom ! I moved the file outside of the convex folder though, since I don't want to export any functions from it.
Thanks a lot for the help!