nextjs checks SERVER ONLY env variables during build (why?) #73642
-
I'm encountering an issue when running a Docker build in a GitHub Action for my Next.js 15.0.3 application. The build process runs without any
Here’s a snippet of the relevant log:
The errors appear because:
These routes are dynamic and shouldn’t be initialized during the build phase. In my setup, I use Kubernetes to build and deploy, where secrets are injected as environment variables at runtime, not during the build process. While I understand that I could provide default values for the Redis port or webhook secret (e.g., |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Unfortunately, Next.js loads the module to do some checks, and because of how Modules work in JS, any top level code is executed, and that's where things might be going south for you. Often you can wrap your top level code under an initializer function, and execute that function within your route handlers, or page rendering. Saving to a top level variable clients, and other objects you might want to reuse later on. |
Beta Was this translation helpful? Give feedback.
Unfortunately, Next.js loads the module to do some checks, and because of how Modules work in JS, any top level code is executed, and that's where things might be going south for you.
Often you can wrap your top level code under an initializer function, and execute that function within your route handlers, or page rendering. Saving to a top level variable clients, and other objects you might want to reuse later on.