How to handle environment variables in JTBDOS.
keys.ts
file, which exports an keys
object that contains all validated environment variables, separated into server
and client
variables.
These keys are then composed together into a single environment variable file for the app, located in an env.ts
file in the root of the app.
.env.example
file is copied to a real environment variable file in each Next.js application and some packages. From here, you can fill in the variables yourself.
Here are the files you will need to fill in:
app/.env.local
- The app’s environment variables.web/.env.local
- The web app’s environment variables.api/.env.local
- The API app’s environment variables.packages/database/.env
- The database package’s environment variables.packages/cms/.env.local
- The CMS package’s environment variables.keys.ts
file contains the validation rules for each environment variable. These are used to validate the environment variables in the .env.local
files. You can inspect these files to see the validation rules for each variable.
sec_
, you should validate it as z.string().min(1).startsWith('sec_')
. This will not only make your intent clearer to anyone reading your code, but will also help prevent errors at runtime.SENTRY_
are automatically added to a Vercel project when you add the Sentry integration from the Vercel Marketplace. Additionally, VERCEL_PROJECT_PRODUCTION_URL
is a very handy environment variable that refers to the “production” URL to which this project is deployed on Vercel.
.env.local
files across the projectserver
or client
object in the relevant package’s keys.ts
file.