node.jstypescriptpostgresqlnext.jsdrizzle

Pg - Next js 15 Module not found: Can't resolve 'fs'


I am using next js 15 and drizzle orm. I have just added drizzle and setup the boilerplate and when i run it i get this error.

Error:

GET /_next/static/webpack/f2b54d6051787b9b.webpack.hot-update.json 500 in 20249ms
 ⚠ Fast Refresh had to perform a full reload due to a runtime error.
 GET /dashboard 500 in 12048ms
 GET /favicon.ico 500 in 34ms
 ⨯ ./node_modules/pg-connection-string/index.js:76:1
Module not found: Can't resolve 'fs'
  74 |
  75 |   // Only try to load fs if we expect to read from the disk
> 76 |   const fs = config.sslcert || config.sslkey || config.sslrootcert ? require('fs') : null
     | ^
  77 |
  78 |   if (config.sslcert) {
  79 |     config.ssl.cert = fs.readFileSync(config.sslcert).toString()      

https://nextjs.org/docs/messages/module-not-found

db file for drizzle

import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";
import { schema } from "@/schema";
import { env } from "@/config/env";


export const client = new Pool({
  connectionString: env.DATABASE_URL,
});

export const db = drizzle(client, { schema, casing: "snake_case" });

Drizzle Configuration file

import { defineConfig, Config } from "drizzle-kit";
import "dotenv";

export default defineConfig({
  dialect: "postgresql",
  schema: "./schema/*",
  dbCredentials: {
    url: process.env.DATABASE_URL!,
  },
  verbose: true,
  strict: true,
  out: "./drizzle/migrations",
  casing: "snake_case",
} as Config);

I even tried changing the client from pg to postgress but i kept getting these module missing errors (some times it shows net module missing).


Solution

  • The modules like fs can be used only on server side. Make sure you are not importing anything on client side from any file that has server side only code.