Open devjiwonchoi opened 3 days ago
Commit: 408b162cee0fb7c54e38d878f438f94c13106587
Is it possible to derive this value from a function provided by an unbundled third party library written in TS? And will this requiredEnv chack run client side?
We have this use case in the State of JS monorepo where we have to manage many variables for multiple apps.
We currently export a getConfig
function from a shared package, that also run a check that all variables are there for the current application. We write our packages in TypeScript and let Next.js bundle them, in order to avoid having 2 builds systems for internal packages that will be bundled anyway. Here is the said shared package (notice our package.json exports map that uses .ts).
It is called during instrumentation and in the root RSC layout so it's called both server-side and client-side.
Maybe a 3rd party package with a more explicit call could be useful, rather than aiming for a Next.js specific function?
@eric-burel I'm currently improving the design of requiredEnv
, would love to hear more about your use case.
Current requiredEnv
check runs on the loaded envs during the build (initial part of the build), which includes all the process.env
values at that span.
So if the value is not loaded during initial part of the build time, it won't be known.
Are the values retrieved from getConfig
won't be known until been called?
Since Next.js by default loads env values from .env
, .env.local
, etc. these values are the target ATM.
I feel like this could be handled if https://github.com/vercel/next.js/pull/63051 is merged, we can import typescript functions like getConfig
and just map the values to the config, right?
@devjiwonchoi the env variables are still loaded as usual using dotenv
files, the getConfig
helpers plays a role quite similar to your proposal by checking if the right environment variables are indeed loaded, except that it works across multiple apps. I've also added some logic to be able to feed import.meta.env
to the system manually, this is needed for Vite-based frameworks such as Astro (up-to-date code is here).
We support optional environment variables as well. The loading of required env variable is synchronous because we get it from a local yaml config, but we could imagine an async system if config is stored into a database.
Why?
In some cases, when the environment variable was missed out during the build, it does not exit which ends up requiring to re-deploy or re-build the app after adding the environment variables.
What?
To prevent this additional steps, this PR added a next config option
requiredEnv
where the users can add the keys of the required environment variables. If the required variable was missed out duringnext build
, it will exit with providing the keys of missing variables.How?
During the build, we check if
requiredEnv
exists, then we check if we have all those required env been loaded. If not, we exit with the warning message.Usage
Set the
requiredEnv
in the next config, that are required to be loaded during build.If the env variable of
OPENAI_API_KEY
was not loaded, next will exit during the build with output: