Next.js Discord

Discord Forum

Set server side env variable to config object

Answered
Barbary Lion posted this in #help-forum
Open in Discord
Barbary LionOP
Answered by Barbary Lion
ill restart IDE to double check

P.S: i did try that before too
View full answer

64 Replies

@Barbary Lion Click to see attachment
if u tend to use this variable only on the clientside u can use NEXTPUBLIC prefix
NEXT_PUBLIC_YOUR_ENV
Barbary LionOP
i use it on server side
and on client?
Barbary LionOP
i used to, but not anymore
if u dont import the env anywhere with the "use client" directive u should be ok
Barbary LionOP
but its not
can u just search for the env?
Barbary LionOP
wym?
search for STRIPE_PREMIUM_MONTHLY_PLAN_ID
Barbary LionOP
acces that env.STRIPE.... directly in server component? yes i could. I mostly want to figure out why im getting this error
also if you put import "server-only" in this file, it should error on the client usages (assuming you also install that package made by react)
@Barbary Lion Click to see attachment
uhm and env.js is some outside script?
nothing to do with nextjs right?
@gin uhm and env.js is some outside script?
Barbary LionOP
from t3 app
uhm
i get it its cooler to have something that automatically switches from prod to dev envs
maybe this is somehow causing the issue idk
@riský also if you put `import "server-only"` in this file, it should error on the client usages (assuming you also install that package made by react)
Barbary LionOP
import "server-only"
add that line to component that importans that config object with env.STRIPE, r? same error
and subscription.ts is really the only file that is importing your const right?
double check
to the file where u defined your const
@gin double check
Barbary LionOP
yes
ok
instead of env
can u try with process.env?
Barbary LionOP
no errors
but im not even importing that function that uses that config
whats premiumPlan?
isnt it your const with the env
Barbary LionOP
yeah
So you are using it
@Barbary Lion no errors
good
so its a issue from this lib
@gin So you are using it
Barbary LionOP
im not importing that function that uses it
You are importing premiumPlan in your file tho
Barbary LionOP
another place where im accessing server env but it works fine
so the compiler executes the logic inside the const
@gin You are importing premiumPlan in your file tho
Barbary LionOP
lemme remove the imports and check it with env.St..
weird, now it works with that..
Barbary LionOP
ill restart IDE to double check

P.S: i did try that before too
Answer
@Barbary Lion weird, now it works with that..
what works? directly using env. without your config?
in your "use server" file
Barbary LionOP
no, the very first implementation i used to have error with
good
i was thinking u probably didnt refresh or recompile or whatever
@gin i was thinking u probably didnt refresh or recompile or whatever
Barbary LionOP
i did restart ts
and dev
yeah it works now
thanks for helping me
nice
no problem!
Barbary LionOP
gl !
u can mark a message as solution so it closes
yeah nvm
or just close it
Barbary LionOP
i dont even know what the solution was :KEKW:
error just disapeared
this probably