Next.js Discord

Discord Forum

process is undefined when access env vars.

Unanswered
Snowshoe posted this in #help-forum
Open in Discord
SnowshoeOP
Hey everyone, I'm trying to add in an analytics key to my Layout.tsx file, please see the image for the code, it's a pretty simple call to process.env, which I'm doing elsewhere for liveblocks and their config file.

I'm using the app router, and followed the instructions (I think) at https://nextjs.org/docs/app/building-your-application/configuring/environment-variables but am still encountering this issue. Is there something special I'm meant to do for the new app router and RSC's?

I'd love any help you can offer!

2 Replies

SnowshoeOP
Bump
Shy Albatross
ENV vars are not available through process on runtime, they get inlined at build time. This means you can't access process as a global variable like you're trying to do inside that Script. You want to either assign the value from process.env toa cons before your return, or interpolate it in JSX with {process.env.NEXT_PUBLIC_SOMETHING}