uwuwushiwu
uwuwushiwu9mo ago

Env variables in schema

I am doing const api_key = process.env.API_KEY which is causing this error
4 Replies
Michal Srb
Michal Srb9mo ago
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:
// schema.ts
const x = process.env.FOO;
export default defineSchema({.....});
// schema.ts
const x = process.env.FOO;
export default defineSchema({.....});
This is OK:
// schema.ts
// Ok because the variable is not evaluated when
// evaluating the schema itself.
export function foo() {
return process.env.FOO;
}
export default defineSchema({.....});
// schema.ts
// Ok because the variable is not evaluated when
// evaluating the schema itself.
export function foo() {
return process.env.FOO;
}
export default defineSchema({.....});
uwuwushiwu
uwuwushiwuOP9mo ago
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?
Michal Srb
Michal Srb9mo ago
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:
function sdk() {
return new SDK(process.env.FOO);
}

// in some function
// instead of
sdk.doSomething()
// do
sdk().doSomething()
function sdk() {
return new SDK(process.env.FOO);
}

// in some function
// instead of
sdk.doSomething()
// do
sdk().doSomething()
erquhart
erquhart8mo ago
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:
Error: Unable to run schema validation on https://<deployment>.convex.cloud
Error fetching POST https://<deployment>.convex.cloud/api/prepare_schema 400 Bad Request: Error: Hit an error while evaluating your schema:
Uncaught Error: Environment variables unsupported when evaluating schema
at setEnvDefaults (../node_modules/@convex-dev/auth/node_modules/@auth/core/lib/utils/env.js:16:12)
at materializeAndDefaultProviders (../node_modules/@convex-dev/auth/dist/server/provider_utils.js:60:4)
at configDefaults (../node_modules/@convex-dev/auth/dist/server/provider_utils.js:24:10)
at convexAuth (../node_modules/@convex-dev/auth/dist/server/implementation.js:206:10)
at <anonymous> (../convex/auth.ts:3:59)
Error: Unable to run schema validation on https://<deployment>.convex.cloud
Error fetching POST https://<deployment>.convex.cloud/api/prepare_schema 400 Bad Request: Error: Hit an error while evaluating your schema:
Uncaught Error: Environment variables unsupported when evaluating schema
at setEnvDefaults (../node_modules/@convex-dev/auth/node_modules/@auth/core/lib/utils/env.js:16:12)
at materializeAndDefaultProviders (../node_modules/@convex-dev/auth/dist/server/provider_utils.js:60:4)
at configDefaults (../node_modules/@convex-dev/auth/dist/server/provider_utils.js:24:10)
at convexAuth (../node_modules/@convex-dev/auth/dist/server/implementation.js:206:10)
at <anonymous> (../convex/auth.ts:3:59)
- 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

Did you find this page helpful?