N
Neon10mo ago
wise-white

```

import { drizzle } from "drizzle-orm/neon-http";
import { neon } from "@neondatabase/serverless";
import { config } from "dotenv";

config({ path: ".env" });
console.log(process.env.DATABASE_URL)
const sql = neon(process.env.DATABASE_URL!);
const db = drizzle({ client: sql });

export default db
import { drizzle } from "drizzle-orm/neon-http";
import { neon } from "@neondatabase/serverless";
import { config } from "dotenv";

config({ path: ".env" });
console.log(process.env.DATABASE_URL)
const sql = neon(process.env.DATABASE_URL!);
const db = drizzle({ client: sql });

export default db
This is index.ts file to create connection but when i call db somewhere else than i get an error Error: No database connection string was provided to neon(). Perhaps an environment variable has not been set? But I have set my env file correctly. Even console log gives correct output
5 Replies
multiple-amethyst
multiple-amethyst10mo ago
Hmm, this is odd, do you get the same error if you hardcode the string?
national-gold
national-gold10mo ago
Yes, when the database connection string is hardcoded, everything works smoothly. I found a solution by adding the following to my next.config.ts
env: {
DATABASE_URL: process.env.DATABASE_URL,
}
env: {
DATABASE_URL: process.env.DATABASE_URL,
}
multiple-amethyst
multiple-amethyst10mo ago
you shouldn't need the dotenv package when using Next.js 🤔
national-gold
national-gold10mo ago
It seems this approach addresses the issue by making DATABASE_URL accessible during both build and runtime, resolving any environment variable loading conflicts. Exactly, but I tried using the dotenv package as part of troubleshooting to resolve the issue.
unwilling-turquoise
unwilling-turquoise10mo ago
doesn't nextjs expect .env.local ?

Did you find this page helpful?