T3 Env - Handle Dev vs Prod

How do you all handle prod vs dev values using t3 env
5 Replies
Joey9
Joey9OP3d ago
like if in prod use "these values" and if in dev use "these values" https://discord.com/channels/719702312431386674/1340673529540247632 im using tanstack start, did the following:
//env.ts
import { createEnv } from "@t3-oss/env-core";
import { z } from "zod";

export const env = createEnv({
server: {
TEST: z.string(),
},
runtimeEnv: process.env,
emptyStringAsUndefined: true,
});
//env.ts
import { createEnv } from "@t3-oss/env-core";
import { z } from "zod";

export const env = createEnv({
server: {
TEST: z.string(),
},
runtimeEnv: process.env,
emptyStringAsUndefined: true,
});
//.env
TEST=test
//.env
TEST=test
then in my app.config.ts import "./env.ts"; and it errors when running pnpm dev
❌ Invalid environment variables: [
{
code: 'invalid_type',
expected: 'string',
received: 'undefined',
path: [ 'TEST' ],
message: 'Required'
}
]
❌ Invalid environment variables: [
{
code: 'invalid_type',
expected: 'string',
received: 'undefined',
path: [ 'TEST' ],
message: 'Required'
}
]
adding dotenv package and doing import "dotenv/config"; in my env.ts made it work would this be the correct approach when handling dev vs prod?
env.NODE_ENV === 'development' ? env.DEV_SUPABASE_URL : env.PROD_SUPABASE_URL
env.NODE_ENV === 'development' ? env.DEV_SUPABASE_URL : env.PROD_SUPABASE_URL
msvcredist2022
shouldn't you have completely separate .env files lol like just make SUPABASE_URL=xxxx in your local .env file and then in your deployment on vercel or your own server or whatever, you configure the env there
Joey9
Joey9OP2d ago
so i should only store local and use prod in my deployment?
The Guy
The Guy2d ago
The only time you should be checking the NODE_ENV is if you're doing some dev or staging specific actions/bypasses (i.e. I bypass webhook verification locally to speed up dev testing, when in a testing env don't do this, etc.). For separating different .env files, most of the hosting platforms like Netlify/Vercel have env vars you can adjust and you can choose between prod, staging/preview, and local dev. The exception is if you're doing a lot of infra orchestration and hosting yourself, but that's a different conversation.
msvcredist2022
you don't need prod keys in your dev environment, and vice versa. you should not have two different ENV keys for this.

Did you find this page helpful?