Next.js Discord

Discord Forum

Error: A node.js API is used

Answered
Boby posted this in #help-forum
Open in Discord
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.
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

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
@B33fb0n3 can you share the filename?
sorry, but what do you mean by sharing the filename?
@Boby sorry, but what do you mean by sharing the filename?
like.. what's the name of the file you executing
@joulev 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
I am just trying to run my application, that above code comes from the drizzle.config.ts
@B33fb0n3
@Boby I am just trying to run my application, that above code comes from the `drizzle.config.ts`
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",
});
@joulev 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", });
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
So I can directly do it without using .env module, with .env.local
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.
but I have used dotenv in past for one of my another next.js project it was working fine.
I was on version 14
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
Okay, I will try doing it and I will get back to you
Thanks for your time and help
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