Next.js Discord

Discord Forum

Error: A node.js API is used

Answered
Boby posted this in #help-forum
Open in Discord
Avatar
I am using the dotenv module and I am getting this error, PFA.
I am on: "next": "^15.0.1",

when I comment out this code, it will work fine.
import { drizzle } from "drizzle-orm/neon-http";
import { neon } from "@neondatabase/serverless";
import * as schema from "./schema"
import { config } from "dotenv";
config({ path: ".env.local" }); // or .env.local
const sql = neon(process.env.DATABASE_URL!);
export const db = drizzle(sql,{schema});

I also tried downgrading the packge to 14.2.3 but I was getting another error that crytpo module can't be bundled something related to dotenv module.
Image
Answered by joulev
Well I don’t know what your past projects looked like. Have you tried my suggestion of simply removing dotenv because it’s doing something nextjs already does for you?
View full answer

21 Replies

Avatar
can you share the filename?
Avatar
does it work if you remove dotenv? you don't need dotenv in nextjs and most likely it's trying to read the .env file which is not allowed in the edge runtime
Avatar
sorry, but what do you mean by sharing the filename?
Avatar
like.. what's the name of the file you executing
Avatar
I am just trying to run my application, that above code comes from the drizzle.config.ts
@B33fb0n3
Avatar
can you share your package.json?
Avatar
drizzle.config.ts should only export drizzle configurations for the drizzle CLI. You need to import neon and declare the db object in a different file
drizzle.config.ts is for drizzle-kit the CLI, not drizzle-orm (yes they are separate, different things and if you don’t need the CLI you don’t need drizzle.config.ts)
An example drizzle.config.ts is this (taken from their documentation):

import { defineConfig } from "drizzle-kit";
export default defineConfig({
dialect: "postgresql",
schema: "./src/schema.ts",
out: "./drizzle",
});
Avatar
I am sorry, it's db/index.ts, I do have a drizzle.config.ts setup correctly in my project, I am able to perform database related tasks as well in my project
Avatar
In db/index.ts, don’t use dotenv.
Avatar
So I can directly do it without using .env module, with .env.local
Avatar
Nextjs automatically loads environment variables to you, and as I said, dotenv reads files which is not allowed in the edge runtime.

Don’t use dotenv there.
Avatar
but I have used dotenv in past for one of my another next.js project it was working fine.
I was on version 14
Avatar
Well I don’t know what your past projects looked like. Have you tried my suggestion of simply removing dotenv because it’s doing something nextjs already does for you?
Answer
Avatar
Okay, I will try doing it and I will get back to you
Thanks for your time and help
Avatar
thanks @joulev it's working now,
I have removed this lines of codeimport { config } from "dotenv"; config({ path: ".env.local" }); // or .env.local
and it's working fine
thanks @B33fb0n3 it's working now