Logo

dev-resources.site

for different kinds of informations.

Load Environment Variables using dotenv-local

Published at
1/8/2025
Categories
dotenv
node
vite
development
Author
yracnet
Categories
4 categories in total
dotenv
open
node
open
vite
open
development
open
Author
7 person written this
yracnet
open
Load Environment Variables using dotenv-local

How to Use dotenv-local to Load Environment Variables for Applications and Environments

dotenv-local is a utility library designed to load environment variables with a specific priority order, ideal for managing configurations across different environments like development, production, or testing. In this tutorial, we will show you how to use dotenv-local to load environment variables for different applications and environments.

Installation

To get started, install the library via npm or yarn:

npm install dotenv-local
# or
yarn add dotenv-local
Enter fullscreen mode Exit fullscreen mode

Usage

For a detailed explanation of the available options, refer to the official repository documentation.

Import the Library

Import loadEnv from dotenv-local:

import { loadEnv } from "dotenv-local";
Enter fullscreen mode Exit fullscreen mode

Example for MongoDB Variables

Let's say you have multiple .env files:

  • .env: Contains generic environment variables.
  • .env.local: Contains local development overrides.
  • .env.production: Contains production-specific variables.
  • .env.production.local: Contains production overrides.

Using dotenv-local, you can ensure that environment variables related to MongoDB are loaded correctly, with the proper overrides depending on your environment.

Basic Usage

Here is a simple example to load all environment variables, without specific prefixes or options:

import { loadEnv } from "dotenv-local";
const envVariables = loadEnv();
console.log(envVariables);
Enter fullscreen mode Exit fullscreen mode

This will load all the environment variables available in the .env files.

Custom Options for MongoDB

Now, let’s load only MongoDB-related environment variables, with custom options:

import { loadEnv } from "dotenv-local";

const { MONGO_URI, MONGO_PASSWORD } = loadEnv({
  envPrefix: "MONGO_",
  envInitial: {
    MONGO_URI: "mongodb://localhost:27017",
    MONGO_PASSWORD: "mongodb://localhost:27017",
  },
  removeEnvPrefix: false,
});
console.log({ MONGO_URI, MONGO_PASSWORD });
Enter fullscreen mode Exit fullscreen mode

This will load only the environment variables that start with MONGO_, and it will not remove the prefix.

Custom Options for Express

Next, let’s load only environment variables related to Express, using a different prefix:

import { loadEnv } from "dotenv-local";

const { HOST, PORT } = loadEnv({
  envPrefix: "DEPLOY_",
  envInitial: {
    DEPLOY_HOST: "127.0.0.1",
    DEPLOY_PORT: "3000",
  },
  removeEnvPrefix: true,
});
console.log({ HOST, PORT });
Enter fullscreen mode Exit fullscreen mode

This will load the variables DEPLOY_HOST and DEPLOY_PORT from your environment files, removing the DEPLOY_ prefix.

Load Multiple Variables for MongoDB and Express

Now, let’s load environment variables related to both MongoDB and Express using different prefixes:

import { loadEnv } from "dotenv-local";

const { MONGO_URI, MONGO_PASSWORD, DEPLOY_HOST, DEPLOY_PORT } = loadEnv({
  envPrefix: ["DEPLOY_", "MONGO_"],
  envInitial: {
    MONGO_URI: "mongodb://localhost:27017",
    MONGO_PASSWORD: "mongodb://localhost:27017",
    DEPLOY_HOST: "127.0.0.1",
    DEPLOY_PORT: "3000",
  },
  removeEnvPrefix: false,
});
console.log({ MONGO_URI, MONGO_PASSWORD, DEPLOY_HOST, DEPLOY_PORT });
Enter fullscreen mode Exit fullscreen mode

This will load the variables DEPLOY_HOST and DEPLOY_PORT from your environment files, as well as MONGO_URI and MONGO_PASSWORD, without removing their prefixes.

Conclusion

Using dotenv-local, you can easily manage environment variables for different applications and environments (e.g., development, production). By defining custom prefixes, initial values, and the loading order, you can ensure that only the relevant variables for MongoDB, Express, or any other service are loaded, making your configuration process clean and efficient.

For more details on available options and how to configure them, check the official documentation.


License

This project is licensed under the MIT License - see the LICENSE file for details.

dotenv Article's
30 articles in total
Favicon
Load Environment Variables using dotenv-local
Favicon
Hashicorp Vault Agent Tutorial: Generating .env from Vault Secrets
Favicon
Flutter Web | Build with .env File
Favicon
Learn .env in Express.js for Beginners (Effortless Setup)
Favicon
How to Hide Only API Keys Instead of Entire Files on GitHub and From Its Commit History
Favicon
Practical Introduction to Environment Variables Using Node.js
Favicon
From dotenv to dotenvx: Next Generation Config Management
Favicon
How to use `.env` file v:20.6.0 `dotenv` npm package do not use.
Favicon
Community Spotlight: David Cochrum
Favicon
Node.js 20.6.0 includes built-in support for .env files
Favicon
What is a .env.vault file
Favicon
Environment variables and configuration anti patterns in Node.js applications
Favicon
Dotenv: Python app environment variable vs. Linux environment variable
Favicon
Node.js includes built-in support for .env files
Favicon
How does python-dotenv simplify Configuration Management?
Favicon
Env::Dot
Favicon
How do you set up .env variables in your NextJS project ?
Favicon
Using ENV file in React & Webpack
Favicon
A simple trick for your dotenv files
Favicon
dotenv and typescript
Favicon
Environment variables & Its best practices
Favicon
Password Manage your environment and secrets with bitwarden
Favicon
5 reasons why your .env environment variables don't work
Favicon
Creating a DotEnv Loader in PHP
Favicon
NextJS - Get rid of DotENV
Favicon
Setting-up a Django project for production
Favicon
Stop using Dotenv in your front-end
Favicon
Supercharge your .env powered projects!
Favicon
Ways to load env variables for your script
Favicon
Doppler: The Triumph and Tragedy of .env Files

Featured ones: