Next.js Discord

Discord Forum

Environment variable not being read for drizzle-kit push.

Answered
VoidPointer posted this in #help-forum
Open in Discord
I thought I'd give Drizzle a try instead of Prisma for a new use case, because it looks simpler. After following their guide, and creating this config, then running drizzle-kit push as per the guide, I get an invalid URL error because my env var isn't being read. I'm guessing this is because the Nextjs runtime isn't reading .env because this runs independenly of it. If I hard code the url here the push works fine. What can I do to somehow get the evn read?

import 'dotenv/config';
import { defineConfig } from 'drizzle-kit';

require('dotenv').config();

export default defineConfig({
  out: './drizzle',
  schema: './src/drizzle/schema.ts',
  dialect: 'postgresql',
  dbCredentials: {
    url: process.env.DATABASE_URL || "",
  },
});

The require is merely the last of many attempts to get the variables read, none of which worked.
Answered by B33fb0n3
try to do it like this:
import dotenv from 'dotenv';
import { defineConfig } from 'drizzle-kit';

dotenv.config();

export default defineConfig({ ... })
View full answer

4 Replies

Answer
@B33fb0n3 try to do it like this: ts import dotenv from 'dotenv'; import { defineConfig } from 'drizzle-kit'; dotenv.config(); export default defineConfig({ ... })
Thanks, I had tried that, as one the many attempts I mentioned, but in response to your suggestion, I tried it again, without joy, then again like this. Looks like an older value was somehow "stuck":
dotenv.config({override: true  });