Drizzle TeamDT
Drizzle Teamβ€’2y ago
Boby

using drizzle with .env.local instead of .env

I am using Drizzle with Next.js, can I use
.env.local
file instead of
.env
or It will throw an error.
Screenshot_515.png
Solution
If /src/db/db.ts is only used from within the Next app, it should just be:
import { drizzle } from "drizzle-orm/neon-http";
import { neon } from "@neondatabase/serverless";

const sql = neon(process.env.DATABASE_URL!);
export const db = drizzle(sql);

There shouldn't be a reason to manually load /.env.local into
process.env
within your Next app source because Next already does this for you - the values from your environment file should already be available on
process.env
without any work on your part.

.env.local
is also intended explicitly for setting environment variables for your local development environment. When you deploy this app to some server, that server should not have an
.env.local
file at all, so it usually doesn't make sense to try to load it in your application code (unless you're doing it conditionally depending on the environment, or some such).

From the project repository you shared in #showcase, it looks like you're deploying to Vercel - you should configure your environment variables for the deployed app through their web interface instead (https://vercel.com/docs/projects/environment-variables), using their special "Sensitive Environment Variables" for critically sensitive values like your database credentials (https://vercel.com/docs/projects/environment-variables/sensitive-environment-variables#create-sensitive-environment-variables).
Was this page helpful?