Next.js Discord

Discord Forum

runtime env with app router and standalone build

Unanswered
Snow Bunting posted this in #help-forum
Open in Discord
Snow BuntingOP
Hey 🙂

I'm struggling to get a runtime environment variable into my standalone build with the app router.

I need the path to my external, non-nextjs backend from the ENV somehow but cannot get it to work. I tried
- NEXT_PUBLIC_MY_PATH but it gets bundled into my standalone output with whatever value it has during build
- using next.config.js and publicRuntimeConfig but the whole config object is undefined more often than not. Also it is not in the docs for the app-router anymore.
- finding a solution on the interwebs but I don't really feel like going the replace-values-inside-standalone-output-files path just yet.

Can anyone lead me into the direction I need to go? 🙂

Thank you so much

7 Replies

European sprat
You're setting an ENV and it can't be read?
Snow BuntingOP
It is read. But the value that is there during build time is always used. No matter if I specify another value during runtime.

I see the values are inlined into the standalone output. Therefor no surprise the value is always the same if the ENV is not read during runtime anymore but used from within the files.
European sprat
Ahh gotcha
Maybe keep an eye on this for when it supports app router https://github.com/expatfile/next-runtime-env
Another way that might work is

FOO=BAR node server.js
Snow BuntingOP
Passing the ENV when starting the node server seems to work in conjunction with using global.process.env to read from rather than without the global - will test this further and update here
Snow BuntingOP
No success so far. global is only available in Node and not the browser where I need the api path to make requests. Any other idea?