Env variables in schema
I am doing const api_key = process.env.API_KEY which is causing this error
4 Replies
As the error suggests, you can't use
process.env.FOO
in the schema module or in any modules imported from the schema.ts
file in the module scope.
So this is bad:
This is OK:
I see. So im trying to integrate an api via an sdk. the sdk allows you to create a client by doing
const sdk = new sdk(process.env.FOO).
All other functions using the sdk would be using sdk.Bar(). Whats a pretty way of only having to create a single sdk rather than making a new sdk object every function?
I think your options are:
1. Don't import the file that uses the SDK into your
schema.ts
2. Create a function that creates the SDK and call it from the functions that need it:
As the error suggests, you can't use process.env.FOO in the schema module or in any modules imported from the schema.ts file in the module scope.Just ran into this, would never have gotten this meaning from the error. What's really weird here: - this is saying that @auth/core is what's causing the problem (which it is: source) - I've commented out the imports from @convex-dev/auth in the schema file at this point and am still getting this error Confirmed - importing
@convex-dev/auth
anywhere in the convex directory triggers this error. cc/ @Michal Srb
Forked and running the example repo, which doesn't have this behavior, comparing
I'm going to have to refactor to verify this, but it looks like use of the auth
object exported by our convex/auth.ts
(eg., for auth.getUserId()
) will cause this issue. Will confirm after refactor. I will do almost anything to avoid moving my entire schema back to that one file.
This ended up being indirectly due to importing things into the schema for me, resolution here: https://discord.com/channels/1019350475847499849/1263545963927044177/1263609867525160972