Next.js
The Next.js package comes preconfigured for Next.js and also enforces some extra rules by default to make sure you have out-of-the-box compatibility in all different Next.js runtimes.
Install dependencies
Install the required dependencies:
@nobara/env-core
requires a minimum of typescript@4.7.2
.
Create your schema
Unlike in the core package, runtimeEnv
is strict by default, meaning you'll have to destructure all the keys manually. This is due to how Next.js bundles environment variables and only explicitly accessed variables are included in the bundle. Missing keys will result in a type-error:
While defining both the client and server schemas in a single file provides the best developer experience, it also means that your validation schemas for the server variables will be shipped to the client. If you consider the names of your variables sensitive, you should split your schemas into two files.
Validate schema on build (recommended)
We recommend you importing your newly created file in your next.config.mjs
. This will make sure your environment variables are validated at build time which will save you a lot of time and headaches down the road.
Use your schema
Then, import the env
object in your application and use it, taking advantage of type-safety and auto-completion: