winsoroaks
winsoroaks16mo ago

weird errors dealing with env vars in convex functions

Hi team! I'm running into some weird errors
// works when client is init in the func
export const sendEmail = action({
handler: async () => {
const resend = new Resend(process.env.RESEND_API_KEY)

await resend.emails.send({
...
})
},
})

// fails when client is init outside of the func
// 400 Bad Request: InvalidModules: Hit an error while pushing:
// Loading the pushed modules encountered the following
// error:
// Failed to analyze purchaseOrderFuncs.js: Uncaught Error: Missing API key. Pass it to the constructor `new // Resend("re_123")`
const resend = new Resend(process.env.RESEND_API_KEY)

export const sendEmail = action({
handler: async () => {
await resend.emails.send({
...
})
},
})

// works when client is init outside of the func but using the real API key
const resend = new Resend("re_MY_API_KEY")

export const sendEmail = action({
handler: async () => {
await resend.emails.send({
...
})
},
})
// works when client is init in the func
export const sendEmail = action({
handler: async () => {
const resend = new Resend(process.env.RESEND_API_KEY)

await resend.emails.send({
...
})
},
})

// fails when client is init outside of the func
// 400 Bad Request: InvalidModules: Hit an error while pushing:
// Loading the pushed modules encountered the following
// error:
// Failed to analyze purchaseOrderFuncs.js: Uncaught Error: Missing API key. Pass it to the constructor `new // Resend("re_123")`
const resend = new Resend(process.env.RESEND_API_KEY)

export const sendEmail = action({
handler: async () => {
await resend.emails.send({
...
})
},
})

// works when client is init outside of the func but using the real API key
const resend = new Resend("re_MY_API_KEY")

export const sendEmail = action({
handler: async () => {
await resend.emails.send({
...
})
},
})
also when i try to use t3-oss/env-nextjs, im getting
node_modules/@t3-oss/env-nextjs/dist/index.d.ts:50:434 - error TS2536: Type 'k' cannot be used to index type 'addQuestionMarks<{ [k_1 in keyof TServer]: TServer[k_1]["_output"]; }, requiredKeys<{ [k_1 in keyof TServer]: TServer[k_1]["_output"]; }>>'.
node_modules/@t3-oss/env-nextjs/dist/index.d.ts:50:434 - error TS2536: Type 'k' cannot be used to index type 'addQuestionMarks<{ [k_1 in keyof TServer]: TServer[k_1]["_output"]; }, requiredKeys<{ [k_1 in keyof TServer]: TServer[k_1]["_output"]; }>>'.
does anyone have any idea? thanks! 🙏
20 Replies
winsoroaks
winsoroaksOP16mo ago
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
ballingt
ballingt16mo ago
@winsoroaks this is intended behavior, environment variables accessed on 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. This isn't expected! Let's figure out what's going on. I don't know what's up with t3-oss/env-nextjs, what's the TypeScript version you're using?
winsoroaks
winsoroaksOP16mo ago
thanks tom! "typescript": "5.2.2", lemme try upgrading my typescript version
ballingt
ballingt16mo ago
that sounds plenty high, I didn't see any issues on the env-nextjs repo about this
winsoroaks
winsoroaksOP16mo ago
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
ballingt
ballingt16mo ago
Is this from using t3-oss/env-nextjs from a module in the convex/ directory? or in the rest of your code
winsoroaks
winsoroaksOP16mo ago
only from the convex/ dir
ballingt
ballingt16mo ago
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
winsoroaks
winsoroaksOP16mo ago
ack. no problem. thanks for clarifying! 🙂
ballingt
ballingt16mo ago
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
winsoroaks
winsoroaksOP16mo ago
yes! that'd be great. i saw that y'all just shipped a shared env stuff. that's def more important that this 😅
ballingt
ballingt16mo ago
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?
ballingt
ballingt16mo ago
@winsoroaks is this environment variable set in the Convex dashboard?
No description
ballingt
ballingt16mo ago
If it weren't, I'd expect deploying the code to work but getting an error later when you actually run the function.
winsoroaks
winsoroaksOP16mo ago
oh weird. im still running locally and havent deployed it were u able to get it running locally? "convex": "^1.3.1",
ballingt
ballingt16mo ago
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.
winsoroaks
winsoroaksOP16mo ago
ok sorry... my bad. my dashboard is empty
ballingt
ballingt16mo ago
so you always need to set these environment variables in the dashboard, try npx convex dashboard to open the right dashboard for your deployment
winsoroaks
winsoroaksOP16mo ago
i just added to the .env.local 🫠 works now!! oh weird, looks like adding client env vars work in env.local
ballingt
ballingt16mo ago
yeah client behavior is based on whatever bundler you're using (guessing Next.js)

Did you find this page helpful?