Using drizzle postgres with trpc in Next.js app

I'm getting the following error with the below code: Module not found: Can't resolve 'dns'. For some reason, my Next.js app is trying to load drizzle on the client where it doesn't have access to Node modules. Has anyone ever run into this before?
import { publicProcedure, router } from "../trpc";
import { z } from "zod";

import { drizzle } from "drizzle-orm/node-postgres";

// If I comment out this function, the error goes away.
export const createDbClient = () => {
return drizzle(process.env.DB_URL!);
};

const formSchema = z.object({
name: z.string().min(1, "Must provide a name."),
email: z.string().email(),
});

export const formRouter = router({
test: publicProcedure.query(() => {
return `test`;
}),
submit: publicProcedure.input(formSchema).mutation(({ input }) => {
// const client = createDbClient();

return `received form values`;
}),
});

export type FormRouter = typeof formRouter;
import { publicProcedure, router } from "../trpc";
import { z } from "zod";

import { drizzle } from "drizzle-orm/node-postgres";

// If I comment out this function, the error goes away.
export const createDbClient = () => {
return drizzle(process.env.DB_URL!);
};

const formSchema = z.object({
name: z.string().min(1, "Must provide a name."),
email: z.string().email(),
});

export const formRouter = router({
test: publicProcedure.query(() => {
return `test`;
}),
submit: publicProcedure.input(formSchema).mutation(({ input }) => {
// const client = createDbClient();

return `received form values`;
}),
});

export type FormRouter = typeof formRouter;
2 Replies
Angelelz
Angelelz3w ago
You are probably importing formRouter in client code. This whole file is only for the server, except for the FormRouter type, that’s the only thing that can be imported in a client file
shandy
shandyOP3w ago
I agree, although I've exhaustively run through the trail of imports and don't see a clear reason 🤔 Is there a good example repo that combines trpc and drizzle in a next.js app? Ah! I had export const runtime = "edge" instead of "nodejs" in my /api/trpc/[trpc]/route.ts. Hope this helps anyone that runs into this issue in the future 🙂
Want results from more Discord servers?
Add your server