Mikael Lirbank
Mikael Lirbank16mo ago

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
Mikael Lirbank
Mikael LirbankOP16mo ago
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.
Mikael Lirbank
Mikael LirbankOP16mo ago
Got this in dev!
'process.env.NODE_ENV' 'development'
'process.env.NODE_ENV' 'development'
Not sure what it will yield in production and preview though.
Indy
Indy16mo ago
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.
Mikael Lirbank
Mikael LirbankOP16mo ago
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.
Indy
Indy16mo ago
I think our packing script added that. But @Tom knows that best. Yep. I'd setup an explicit env variable for that.
Mikael Lirbank
Mikael LirbankOP16mo ago
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...
Indy
Indy16mo ago
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.
Mikael Lirbank
Mikael LirbankOP16mo ago
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...
ballingt
ballingt16mo ago
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 cleaner
Mikael Lirbank
Mikael LirbankOP16mo ago
I 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...
ballingt
ballingt16mo ago
where url.ts has that variable normally set to localhost:3000 or whatever
Mikael Lirbank
Mikael LirbankOP16mo ago
Ah! 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!

Did you find this page helpful?