weird errors dealing with env vars in convex functions
Hi team! I'm running into some weird errors
also when i try to use t3-oss/env-nextjs, im getting
does anyone have any idea? thanks! 🙏
20 Replies
i've searched around and came across tom's work: https://github.com/thomasballinger/convex-html-email-example/blob/main/convex/myFunctions.tsx. tom was hard coding the API key there.
looks like im running into the same issue in this thread: https://discord.com/channels/1019350475847499849/1161498699558559754
process.env.MY_ENV_VAR
are currently undefined
at push time. This means you'll need to create the Resend object inside a function or hardcode the credential as you've seen.thanks tom!
"typescript": "5.2.2",
lemme try upgrading my typescript version
that sounds plenty high, I didn't see any issues on the env-nextjs repo about this
got it. i just npm install'ed t3-oss/env-nextjs today. that should be up to date. unless it's messing with nextjs 14
Is this from using t3-oss/env-nextjs from a module in the convex/ directory? or in the rest of your code
only from the convex/ dir
we should look into this, but without trying it I'm guessing env-nextjs is not a good fit for the convex dir: different environment variables will be available than in Next.js (Convex requires entering environment variables in the dashboard for that deployment, it doesn't bundle any in or automatically sync a .env.local) and way env-nextjs works might be specific to Next.js
ack. no problem. thanks for clarifying! 🙂
I hear you that some kind of typesafe way to deal with environment variables would be convenient, perhaps it's adding support for Convex to env-nextjs or doing something more intetrated
yes! that'd be great. i saw that y'all just shipped a shared env stuff. that's def more important that this 😅
always helpful to be reminded what people's standards are! It's easy to think e.g. types for environment variables is a cutting edge nicety developers don't expect and deprioritize it, so thanks for reporting
@winsoroaks it looks like I was wrong, environment variables should be available and we're not able to reproduce this!
const resend = new Resend(process.env.RESEND_API_KEY)should be ok, anything else unusual here? I don't think this behavior should be influenced by this, but what version of Convex are you using?
@winsoroaks is this environment variable set in the Convex dashboard?

If it weren't, I'd expect deploying the code to work but getting an error later when you actually run the function.
oh weird. im still running locally and havent deployed it
were u able to get it running locally?
"convex": "^1.3.1",
In Convex code never really runs locally (although we're working on open sourcing parts of it to make that possible someday!), it's always running on a Convex server.
ok sorry... my bad. my dashboard is empty
so you always need to set these environment variables in the dashboard, try
npx convex dashboard
to open the right dashboard for your deploymenti just added to the .env.local 🫠
works now!!
oh weird, looks like adding client env vars work in env.local
yeah client behavior is based on whatever bundler you're using (guessing Next.js)