'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'
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 });
}
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....
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>
<script lang="ts">
import { userCreateSchema } from '@myrepo/database';
</script>
Simply importing the create schema makes it pop the error. Any help is appreciated.
1 Reply
Lick A Brick
Lick A BrickOP7d ago
Hmm.. I found https://discord.com/channels/1043890932593987624/1187446556782239816/1187448962005872660 which makes me believe this is not the best way to do this. I might just define the validation schemas in my frontend.

Did you find this page helpful?