Add or update env variables
Why?
You may want to edit or add new environment variables, that are essential for enhancing the security and flexibility of an application. They keep sensitive information, like API keys and passwords, out of the codebase, reducing the risk of data leaks. Moreover, they allow configurations to be easily changed between different environments (development, testing, production) without altering the code, making the application more adaptable and maintainable.
How?
Environment variables stands in the
.envfile, at the root of your project, which isn't versioned in the git repository. The .env file is created when you execute the "getting started" steps of Start UI's README.md
Edit an environment variable
- Change the value in the
.envfile and save the file - Depending on the environment variable, the change could take effect immediately
- If necessary, change the example value in the
.env.exampleto spread the right default values to your colleagues
Add an environment variable
- Create a new environment variable in the
.env. Note that environment variables should be prefixed byNEXT_PUBLIC_if you want them to be accessible in the front-end. - Since environment variables are validated with zod, you need to add it to the
src/env.mjs:- In the client or server section, depending on where it will be used
- Also in the
runtimeEnvsection
- To use the values, import the
envvariable like this and use the variable of your choice
import { env } from '@/env.mjs';
const BASE_URL = env.NEXT_PUBLIC_BASE_URL;
const LOG_LEVEL = env.LOGGER_LEVEL;- Don't forget to add the new variable to the
.env.exampleto help your colleagues when they'll set up their.env