Gathering detailed insights and metrics for dotenv-mono
Gathering detailed insights and metrics for dotenv-mono
Gathering detailed insights and metrics for dotenv-mono
Gathering detailed insights and metrics for dotenv-mono
This package permit to have a centralized dotenv on a monorepo. It also includes some extra features such as manipulation and saving of changes to the dotenv file, a default centralized file, and a file loader with ordering and priorities.
npm install dotenv-mono
Typescript
Module System
Node Version
NPM Version
85.6
Supply Chain
100
Quality
84.9
Maintenance
100
Vulnerability
100
License
TypeScript (86.59%)
JavaScript (13.41%)
Total Downloads
1,582,199
Last Day
888
Last Week
12,846
Last Month
56,822
Last Year
819,393
GPL-3.0 License
46 Stars
285 Commits
4 Forks
1 Watchers
9 Branches
6 Contributors
Updated on Aug 01, 2025
Latest Version
1.4.0
Package Id
dotenv-mono@1.4.0
Unpacked Size
229.99 kB
Size
42.71 kB
File Count
34
NPM Version
10.9.2
Node Version
22.13.1
Published on
Jul 17, 2025
Cumulative downloads
Total Downloads
Last Day
-5.6%
888
Compared to previous day
Last Week
-1.9%
12,846
Compared to previous week
Last Month
-23.7%
56,822
Compared to previous month
Last Year
14%
819,393
Compared to previous year
4
23
To prevent code duplication and enhance re-usability, a centralized configuration including all of your environment variables might be handy.
Rather of generating a .env
file for each package, we may utilize a single .env
file at the project's root.
This is a package that allows monorepo applications and packages to share and load a centralized dotenv. It's based over dotenv package.
It also includes some extra features such as manipulation and saving of changes to the dotenv file, a default centralized file, and a file loader with ordering and priorities.
The plugin dotenv-expand is enabled by default.
1├── .env 2├── .env.production 3├── .env.defaults 4├── packages 5│ ├── ui-library 6│ ├── other-library 7├── apps 8│ ├── web 9│ │ ├── .storybook 10│ ├── docs
The package search the first .env
file, matching with some priority criteria, by walking up the parent directories.
Starting from the current process directory, this package finds the first file that matches the best filepath and filename criteria with the highest priority. The greater the depth of the up folder, the lesser its priority.
The priority can be customized on the configuration with the priorities
property, see the example below on
the usage section.
Note: The allowed values for
NODE_ENV
are usuallytest
,development
andproduction
.
Priority | Filename |
---|---|
75 | .env.$(NODE_ENV).local |
50 | .env.local |
25 | .env.$(NODE_ENV) |
1 | .env |
Given the following folder structure with dotenv files:
1├── .env 2├── .env.production 3├── apps 4│ ├── .env.development 5│ ├── web 6│ ├── docs 7│ │ ├── .env 8│ │ ├── .env.local
Having the following priority order:
Path | Priority | Depth |
---|---|---|
.env | 1 | 2 |
.env.production | 25 | 2 |
apps/.env.development | 25 | 1 |
apps/docs/.env | 1 | 0 |
apps/docs/.env.local | 50 | 0 |
Then we will have the following outcome scenarios:
Current working directory | Env | Match |
---|---|---|
/ | development | .env |
/ | production | .env.production |
apps/web | development | .env |
apps/web | development | apps/.env.development |
apps/docs | development | apps/docs/.env.local |
Install the library from npm or yarn just running one of the following command lines:
npm | yarn |
---|---|
npm install dotenv-mono --save | yarn add dotenv-mono |
For custom advanced configuration of Next.js, you can create a next.config.js
or next.config.mjs
file in the root of
your project directory (next to package.json
).
Add the following line at the top of the file:
1require("dotenv-mono").load();
1require("dotenv-mono").load(); 2 3/** 4 * @type {import('next').NextConfig} 5 */ 6const nextConfig = { 7 /* config options here */ 8}; 9 10module.exports = nextConfig;
The main configuration file is .storybook/main.js
. This file controls the Storybook server's behavior, so you must restart Storybook’s process when you change it.
Add the following lines on the file:
1const dotenv = require("dotenv-mono").load(); 2 3const config = { 4 /* config options here */ 5 env: (config) => { 6 return { 7 ...config, 8 ...dotenv.env, 9 }; 10 }, 11}; 12 13module.exports = config;
Simple methods to export environment variables from the dotenv into the working process. Here are several potential implementation approaches based on your preferences.
1// Inline 2require("dotenv-mono").load(/* config */); 3 4// Using the function 5const {dotenvLoad} = require("dotenv-mono"); 6dotenvLoad(/* config */); 7 8// Using import 9import {dotenvLoad} from "dotenv-mono"; 10const dotenv = dotenvLoad(); // Dotenv instance 11 12// Using the class 13const {Dotenv} = require("dotenv-mono"); 14const dotenv = new Dotenv(/* config */); 15dotenv.load();
If you need a fast way to replace dotenv package with dotenv-mono, and you need also to have a retro-compatible feature, you can have back directly the output like dotenv package using the config
method.
1// Inline 2const output = require("dotenv-mono").config(/* config */); 3 4// Using the function 5const {dotenvConfig} = require("dotenv-mono"); 6const output = dotenvConfig(/* config */);
1// Use `.dotenv.server` or `.dotenv.server.local`, etc... 2load({extension: "server"});
1// You can specify the file path 2load({path: "../../configs/.env"});
dotenv-expand
extension1load({expand: false});
1load({defaults: ".env.def"});
1// If `.dotenv.overwrite` is present use it with max priority 2load({ 3 priorities: { 4 ".env.overwrite": 100, 5 }, 6});
1const dotenv = require("dotenv-mono").load(); 2dotenv.save({"MY_ENV_1": "enjoy"}); 3 4// Without loading into the working process 5const {Dotenv} = require("dotenv-mono"); 6const dotenv = new Dotenv(); 7dotenv.loadFile(); // Skip loading into the process 8dotenv.save({ 9 "MY_ENV_1": "enjoy", 10 "MY_ENV_2": "'enjoy quotes'", 11 "MY_ENV_3": 999, 12});
As on the dotenv package on the CLI/Console, you can use the --require
(-r
) command line option to preload dotenv. By doing this, you do not need to require and load dotenv in your application code.
1$ node -r dotenv-mono/load your_script.js
The configuration options below are supported as command line arguments in the format dotenv_config_<option>=value
1$ node -r dotenv-mono/load your_script.js dotenv_config_path=/custom/path/to/.env dotenv_config_debug=true
Additionally, you can use environment variables to set configuration options. Command line arguments will precede these.
1$ DOTENV_CONFIG_<OPTION>=value node -r dotenv-mono/load your_script.js
1$ DOTENV_CONFIG_ENCODING=latin1 DOTENV_CONFIG_DEBUG=true node -r dotenv-mono/load your_script.js dotenv_config_path=/custom/path/to/.env
You can use dotenv-mono
as a command-line tool to run commands with environment variables loaded from your .env
files. This is similar to dotenv-cli
but with all the enhanced features of dotenv-mono
.
1# Basic usage - load .env and run a command 2dotenv-mono -- node your_script.js 3 4# Load specific .env file 5dotenv-mono -e .env.production -- node your_script.js 6 7# Load multiple .env files 8dotenv-mono -e .env.local -e .env.production -- node your_script.js 9 10# Set additional variables 11dotenv-mono -v NODE_ENV=production -v DEBUG=true -- node your_script.js 12 13# Print a variable value 14dotenv-mono -p NODE_ENV 15 16# Debug mode - see what files would be loaded 17dotenv-mono --debug -e .env.production 18 19# Use all dotenv-mono features 20dotenv-mono --cwd /path/to/project --extension server --depth 3 -- node server.js
Option | Description |
---|---|
--help | Print help message |
--debug | Output the files that would be processed but don't actually parse them or run the command |
-e <path> | Parse the file <path> as a .env file and add variables to the environment (multiple allowed) |
-v <name>=<value> | Put variable <name> into environment using <value> (multiple allowed) |
-p <variable> | Print value of <variable> to the console |
--no-expand | Skip variable expansion |
--override | Override system variables |
--cwd <path> | Specify the current working directory |
--depth <number> | Specify the max depth to reach when finding up the folder tree |
--encoding <enc> | Specify the encoding of your file containing environment variables |
--extension <ext> | Specify to load specific dotenv file used only on specific apps/packages |
--defaults <file> | Specify the defaults dotenv filename |
--priorities <json> | Specify the criteria of the filename priority to load as dotenv file |
Setting | Description | Default |
---|---|---|
cwd | Specify the current working directory | process.cwd() |
debug | Turn on/off logging to help debug why certain keys or values are not being set as you expect | false |
defaults | Specify the defaults dotenv filename (it can't override any environment variables) | .env.defaults |
depth | Specify the max depth to reach finding up the folder from the children directory | 4 |
encoding | Specify the encoding of your file containing environment variables | utf8 |
expand | Turn on/off the dotenv-expand plugin | true |
extension | Specify to load specific dotenv file used only on specific apps/packages (ex. .env.server... ) | |
override | Override any environment variables that have already been set on your machine with values from your .env file | false |
path | Specify a custom path if your file containing environment variables is located elsewhere | |
priorities | Specify the criteria of the filename priority to load as dotenv file | See Priorities |
It will read your .env
file following the criteria, parse the contents, assign it to process.env
.
Note: This method differs from the previous
load
function. In that it requires the configuration to be loaded on the class instance via the constructor.
public load(loadOnProcess: boolean): Dotenv;
It will read your .env
file following the criteria, parse the contents, ready to be read or changed programmatically.
public loadFile(): Dotenv;
Merge the data on input with the loaded data from load
or loadFile
, and save the changes on the original dotenv file.
Note: If
.env.defaults
is present, it won't be overwritten, you can just save the changes on the main dotenv file (.env
,.env.local
, etc...)
public save(changes: Record<string, any>): Dotenv;
See the dotenv documentation HERE
public parse<T extends Record<string, any> = Record<string, any>>(src: string | Buffer): T;
Have an idea? Found a bug? Please raise to ISSUES or PULL REQUEST. Contributions are welcome and are greatly appreciated! Every little bit helps, and credit will always be given.
No vulnerabilities found.