env variable is undefined

Hi people, I'm having issue that my env variables in config.drizzle.ts are being undefined, even though they exist in my .env file, If i try to hardcode them, they work fine. if I try to console log them in server component they have expected value
import type { Config } from "drizzle-kit";

export default {
schema: "./src/server/db/schema.ts",
out: "migrations",
driver: "pg",
breakpoints: true,
dbCredentials: {
host: process.env.DB_HOST || "",
port: 5432,
ssl: true,
database: process.env.DB_NAME || "",
user: process.env.DB_USER || "",
password: process.env.DB_PASSWORD || "",
},
} satisfies Config;
import type { Config } from "drizzle-kit";

export default {
schema: "./src/server/db/schema.ts",
out: "migrations",
driver: "pg",
breakpoints: true,
dbCredentials: {
host: process.env.DB_HOST || "",
port: 5432,
ssl: true,
database: process.env.DB_NAME || "",
user: process.env.DB_USER || "",
password: process.env.DB_PASSWORD || "",
},
} satisfies Config;
5 Replies
Bozic0909
Bozic090914mo ago
I solved it by using
import * as dotenv from 'dotenv';

dotenv.config({
path: '.env.local',
});
import * as dotenv from 'dotenv';

dotenv.config({
path: '.env.local',
});
at the top of the file
jeff.kershner
jeff.kershner14mo ago
Not sure if this is an older version of t3, but in the latest at the time of my post (7.20.2), the drizzle.config.ts looks like this out of the box (and works):
import { type Config } from "drizzle-kit";

import { env } from "~/env.mjs";

export default {
schema: "./src/server/db/schema.ts",
driver: "mysql2",
dbCredentials: {
connectionString: env.DATABASE_URL,
},
tablesFilter: ["rtc_*"],
} satisfies Config;
import { type Config } from "drizzle-kit";

import { env } from "~/env.mjs";

export default {
schema: "./src/server/db/schema.ts",
driver: "mysql2",
dbCredentials: {
connectionString: env.DATABASE_URL,
},
tablesFilter: ["rtc_*"],
} satisfies Config;
Bozic0909
Bozic090914mo ago
I'm not using T3
jeff.kershner
jeff.kershner14mo ago
Isn't this a t3 discord?
Bozic0909
Bozic090913mo ago
as far as I know its not, you can ask question about anything related to typescript web next react etc
Want results from more Discord servers?
Add your server