I am using Node.js and Zod to parse environment variables. When I run npm run start, the following script runs:
import { z } from 'zod';
const EnvSchema = z.object({
SOME_KEY: z.string(),
});
export type Env = z.infer<typeof EnvSchema>;
const parsed = EnvSchema.safeParse(process.env);
if (!parsed.success) { console.error(
'❌: Invalid environment variables:',
JSON.stringify(parsed.error.format(), null, 4),
);
process.exit(1);
}
// ... start API server
The script errors, saying the environment variables don’t exist even though I’ve added them in Render. Are these environment variables not available immediately at runtime?
When I log process.env, I don’t see any of the environment variables I specified.
Environment variables are available once they have been saved and a deployment has occurred. Can you confirm if you see a deploy occurring once you’ve saved them, or are you triggering a manual deploy once the vars have been added?
What do you mean by “a deployment has occurred?” I’m trying to get my first deploy up and running, and I’m triggering the deploy manually right now (after I’ve already added the environment variables)
When you change environment variables a new deployment of the service has to be done - by default this is automatic but you can change services not to auto deploy so you need to manually trigger a deployment - I was highlighting this in case this was the case.
If you’re using a paid instance type, you can use the shell tab to inspect the environment variables that are set - if they show there then it’s your application code that is not referencing them correctly. If you’re using a free instance type you don’t have access to shell so you’d need to show the variable during the build/deploy step to confirm they are present as you expect them to be.
Hey yes, I’m doing a manual deployment after changing the environment variables. I’m also printing the environment variables during the run step (I do a console.log(process.env); and the variables are not printed. See the last screenshot in the original post. That’s why I was wondering when the environment variables are set
Okay I figured it out. It looks like it was an issue with Turborepo pruning the environment variables under strict mode. The solution was to use loose mode (Using environment variables | Turborepo).