Drizzle TeamDT
Drizzle Team10mo ago
1 reply
Lick A Brick

'process is not defined' in Sveltekit SPA

Hi, I have a monorepo (turborepo) and I have my drizzle instance as a package. In my schema I have createInsertSchema I want to use in my frontend to do form validation. The problem is I get the error process is not defined because the code is ran from the client. In FF devtools I see it goes wrong at user: process.platform === 'win32' ? process.env.USERNAME : process.env.USER, (line 8 @ http://localhost:5173/node_modules/pg/lib/defaults.js). Why does it need pg in the first place? I only import the createInsertSchema.

Here is the code:

/// Database package:

packages\database\index.ts
import initDb from "./database";

export * from "./schema";
export default initDb
export * from 'drizzle-orm'

packages\database\database.ts
import { drizzle } from "drizzle-orm/node-postgres";

import * as schema from "./schema";

export default function initDb(databaseUrl: string) {
    if (!databaseUrl) {
        throw new Error("A valid DATABASE_URL must be provided.");
    }
    return drizzle(databaseUrl, { schema });
}


packages\database\schema.ts
import { relations } from "drizzle-orm";
import { pgTable, text, integer, timestamp, boolean } from "drizzle-orm/pg-core";
import { createInsertSchema, createSelectSchema, createUpdateSchema } from "drizzle-zod";
import { nanoid } from "nanoid";
import { z } from "zod";

export const user = pgTable("user", {
    id: text("id").primaryKey(),
    name: text('name').notNull(),
    email: text('email').notNull().unique(),
    emailVerified: boolean('email_verified').notNull(),
    image: text('image'),
    createdAt: timestamp('created_at').notNull(),
    updatedAt: timestamp('updated_at').notNull()
});

.... nothing special should follow....


//// My Sveltekit app

The code lives in a component e.g. myForm.svelte

<script lang="ts">
    import { userCreateSchema } from '@myrepo/database';
</script>


Simply importing the create schema makes it pop the error. Any help is appreciated.
Was this page helpful?