Next.js Discord

Discord Forum

Issue Accessing Environment Variables Set in Systemd Service File in Next.js on Ubuntu Server

Unanswered
Pip gall wasp posted this in #help-forum
Open in Discord
Pip gall waspOP
Hello everyone! I'm encountering an issue with my Next.js application hosted on an Ubuntu server. I have set some environment variables within my systemd service file:

[Service]
...
Environment=NEXT_PUBLIC_SYSTEM_ENV=Test
Environment=NEXT_PUBLIC_API_URL=https://agrohub-api.lemon.do
...

However, when trying to access these variables in my Next.js app using process.env.NEXT_PUBLIC_SYSTEM_ENV and process.env.NEXT_PUBLIC_API_URL, I'm getting undefined.

I've tried reloading systemd and restarting the service, verifying the environment of the running process, and ensuring there's no conflict within any configuration files. Yet, the issue persists.

Has anyone faced a similar issue or can offer any insights on how to resolve this? Any help would be greatly appreciated!

6 Replies

Pip gall waspOP
As far as I know, keeping .env files in the project (on Git) is not considered best practice. However, when I tried to add a .env.production file directly on the server, it gets deleted every time I pull a new version from Git and rebuild.
Dunker
Well, not like that actually
Basically your vps is your new localhost at cloud, so you can put it in your repo no one can see it
Also hosting services giving an interface to expose your env values, i dk you are using which @Pip gall wasp
I suggest deploying your app to some host have GitHub integration instead plain vps, like digital ocean, vercel etc