Warning
This project is under development and is not yet ready for use.
The .env files are used to define environment variables for the project. The following files are used to manage configurations for different environments, such as development, testing, and production.
This structure enables a clear and organized approach to managing configurations, making it easier to customize settings for specific environments while maintaining a consistent setup for shared configurations
-
process.envvariables -
.envand.env.localfiles -
.env.${NODE_ENV}and.env.${NODE_ENV}.localfiles - Default, required and strict values
- Get value as array, boolean, number, string
- Get value as date, object, url
The following tools were used in the construction of the project:
Use the package manager npm, yarn.
npm install @geisonjr/envfyyarn add @geisonjr/envfyTip
Both .env and .env.local files are loaded across all environments, providing a foundation for common configurations.
Important
These base configuration files reside in the root of the project.
Important
The prority of the environment variables is as follows:
process.env- default environment variables from theOS.env- overridesprocess.env.env.local- overrides.env.env.${NODE_ENV}- overrides.env.local.env.${NODE_ENV}.local- overrides.env.${NODE_ENV}
.env: Default environment variables.
The .env* files are selected based on the NODE_ENV environment variable, allowing tailored configurations for different deployment scenarios.
.env.${NODE_ENV}: Environment-specific settings.
In the following example, the .env.development file is used to define environment variables for the development environment when the NODE_ENV environment variable is set to development.
NODE_ENV=development# .env.development
ENV_VAR_STRING=development-value
ENV_VAR_NUMBER=456The .env*.local files are used to override configurations for local development, providing a way to customize settings without affecting the shared configurations.
Caution
The .env*.local files are not committed to the version control system, as they are intended for local overrides. This practice ensures that the local environment settings do not interfere with the shared configurations.
Create a .env file in the root of your project and add your environment variables.
ENV_VAR_STRING=my-value // Can also use double quotes
ENV_VAR_NUMBER=123 // Can also use floating point numbers
ENV_VAR_BOOLEAN=true // "true", "yes", "y", "1", "on" are considered trueTip
The following types are supported:
array- Can also use double quotes1;2;3or"1;2;3"
boolean- Can also use double quotes, Case insensitive- Truthy:
true,yes,y,1,on. - Falsy:
false,no,n,0,off.
- Truthy:
number- Can also use double quotes123123,456.789
string- Can also use double quotesmy-valueor"my-value"
Import the package in your code and use the environment variables.
import '@geisonjr/envfy/config'
// Environment variables are now available in process.env
process.env.ENV_VAR_STRING // 'my-value'
process.env.ENV_VAR_NUMBER // '123'
process.env.ENV_VAR_BOOLEAN // 'true'import * as envfy from '@geisonjr/envfy'
// Get value as boolean
envfy.boolean('ENV_VAR_BOOLEAN') // true
// Get value as number
envfy.number('ENV_VAR_NUMBER') // 123
// Get value as string
envfy.string('ENV_VAR_STRING') // 'my-value'import * as envfy from '@geisonjr/envfy'
// Get value as boolean with default value
envfy.boolean('ENV_VAR_NOT_EXISTS', true) // true
// Get value as number with default value
envfy.number('ENV_VAR_NOT_EXISTS', 123.45) // 123.45
// Get value as string with default value
envfy.string('ENV_VAR_NOT_EXISTS', 'default-value') // 'default-value'This project is under the MIT License