client.mjs, server.mjs, process.env

Can someone please explain the use cases for each? I see that process.env in used in src/utils/trpc.ts. Why not use server.mjs there? In server,mjs we do ...clientEnv which means in server.mjs we have all the env variables. Then when should we use client.mjs?
41 Replies
Pod
Pod2y ago
The env exports dont contain the port or vercel url which is used in that file, please don't make duplicate posts
shikishikichangchang
So if I add them to server.env I can use them? I don't understand the need for client.mjs if server.mjs has all the env vars we need?
Pod
Pod2y ago
Client only includes what will be exposed client side So you import it on the client, and server on the server
shikishikichangchang
i can import server on the client and it would still work
Pod
Pod2y ago
Anything not prefixed with NEXT_PUBLIC won't be exposed to the client in production
shikishikichangchang
export const env = { ..._serverEnv.data, ...clientEnv }; That line in server.mjs exposes any NEXT_PUBLIC-prefixed env vars
Pod
Pod2y ago
If you import server envs on the client, server variables will be undefined, and it'll probably error, if not then they will be nothing
shikishikichangchang
How does nextjs know I'm importing server.mjs or client.mjs? Is everything under "server" considered server-side? What about "api/"? How does NextJS know which object exposes what env variables?
Pod
Pod2y ago
Basic Features: Environment Variables | Next.js
Learn to add and access environment variables in your Next.js application.
Pod
Pod2y ago
It doesn't But it will expose anything with NEXT_PUBLIC to the client, and anything else plus that to the server
shikishikichangchang
By that logic If I use import { env } from "../../env/server.mjs"; on the client Why would it throw an error?
Pod
Pod2y ago
Because the zod parsing would not pass
shikishikichangchang
Which one? serverSchema?
Pod
Pod2y ago
The server
shikishikichangchang
Why would it not pass? Isn't it seeing .env?
Pod
Pod2y ago
Because the server-sided environment variables would not exist, since its the client
shikishikichangchang
I see So how does NextJS defines what is "the client"?
Pod
Pod2y ago
The browser The react part of the nextjs app
shikishikichangchang
In our file structure, everything in the server folder is not "the client" right?
Pod
Pod2y ago
It depends where its ran from, in this case all that would be server
shikishikichangchang
hmm what if I need a variables in both under pages/ and under server? I know now that I cannot use server.mjs on the client side since those .env vars would not be seen by the browser, would it work if I try to use client.mjs under server/ or under api/? I tried it and it seems to work, but it seems a little unconventional to do that?
Pod
Pod2y ago
Then they should be setup as client variables
shikishikichangchang
Yes they are set up as client vars
Pod
Pod2y ago
Importing client on the server would work but no reason to
shikishikichangchang
but i also need them as server vars I need the NEXT_PUBLIC_MY_VAR on both pages/ and server/
Pod
Pod2y ago
Client vars will be on both as you showed above
shikishikichangchang
What's the best way to do it? I know process.env.NEXT_PUBLIC_MY_VAR and env.NEXT_PUBLIC_MY_VAR both work should I import { env } from "../../env/client.mjs";?
Pod
Pod2y ago
On the client yes You import the appropriate env file for your environment
shikishikichangchang
So you're saying under pages/ I should import import { env } from "../../env/client.mjs"; And under server I should import import { env } from "../../env/server.mjs";
Pod
Pod2y ago
Yep And API routes
shikishikichangchang
Ok...the reason I'm asking is because I have an object that is shared both by the server and the client Is there ever a reason to use process.env? Like in src/utils/trpc.ts Why is that file not using client.mjs or server.mjs?
Pod
Pod2y ago
It's used there because vercel url and port don't always exist and they're not managed by the user
shikishikichangchang
Are VERCEL_URL and PORT and NODE_ENV special variables managed by Vercel?
Pod
Pod2y ago
Yes Not node env
shikishikichangchang
PORT is managed by vercel?
Pod
Pod2y ago
Yes, they add that You can also add it in dev if you want but there's the default hard coded in
shikishikichangchang
For NODE_ENV, I didn't specify it in my .env file but it seems to have a value somehow. Does NodeJS do that automatically? Or NextJS? I know that its value is "development" when I run "npm run dev" and "production" after "npm run build"
Pod
Pod2y ago
Node does that
shikishikichangchang
I see If vercel adds the port Looking at this http://localhost:${process.env.PORT} It means vercel uses http://localhost for SSR? Oh wait nvm Ny bad So Vercel uses https://${process.env.VERCEL_URL} for SSR whereas when developing we use localhost 3000 So I think PORT is managed by us not Vercel
Pod
Pod2y ago
Oh yeah, that's not used on vercel deployments In there in case you want to use a custom port while developing
shikishikichangchang
Thank you I appreciate the clarification
Want results from more Discord servers?
Add your server