T
TanStack4mo ago
stuck-chocolate

Server side library running on client

Hello, I am a tanstack start begginer. I am trying to switch from Prisma to Drizzle ORMs because I was facing a weird issue with Prisma. My server code requires .env variables, but the client browser is trying to execute the same code, which causes of course an issue. I am trying to do this:
import 'dotenv/config';
import { drizzle } from 'drizzle-orm/libsql';

const db = drizzle(process.env.DATABASE_URL!);

export default db;
import 'dotenv/config';
import { drizzle } from 'drizzle-orm/libsql';

const db = drizzle(process.env.DATABASE_URL!);

export default db;
But in my browser, I am getting an error:
TypeError: args is undefined
optionMatcher cli-options.js:4
<anonymous> config.js:6
<anonymous> config.js:9
...
TypeError: args is undefined
optionMatcher cli-options.js:4
<anonymous> config.js:6
<anonymous> config.js:9
...
Clicking the cli-options link shows that the dotenv library is being executed on the client 😅 I was taking a look at this repo: https://github.com/aaronksaunders/tanstack-start-drizzle-app/tree/main He does the same as I do except that his database file path is hardcoded https://github.com/aaronksaunders/tanstack-start-drizzle-app/blob/e867a96e3e1097018c64b7f9cccf93d34b1b6c9d/drizzle/db.ts#L10C1-L11C17 ... So my question would be, how am I supposed to "split" the server side code and the client one in this case? I have to import my db variable to be used in the server functions
2 Replies
national-gold
national-gold4mo ago
where can you show a complete example? or is this the repo you linked to? if you only use db inside of server functions, it will be split by start automatically
stuck-chocolate
stuck-chocolateOP4mo ago
Oh dang... I have realized I was trying to add GraphQL to my project, I am importing and using the db variable in the GraphQL Mutation resolver 😵‍💫 if I comment out that part the error dissapears ... I will try to figure out how to use graphql and server functions properly ...

Did you find this page helpful?