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.env file for each package, we may utilize a single .env file at the project's root.
text
βββ .env
βββ .env.production
βββ .env.defaults
βββ packages
β βββ ui-library
β βββ other-library
βββ apps
β βββ web
β β βββ .storybook
β βββ docs
`
#### How it works?
The package search the first .env file, matching with some priority criteria, by walking up the parent directories.
##### Priorities
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 usually test, development and production.
| Priority | Filename |
| -------- | ------------------------ |
| 75 | .env.$(NODE_ENV).local |
| 50 | .env.local |
| 25 | .env.$(NODE_ENV) |
| 1 | .env |
###### Example
Given the following folder structure with dotenv files:
`text
βββ .env
βββ .env.production
βββ apps
β βββ .env.development
β βββ web
β βββ docs
β β βββ .env
β β βββ .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
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 |
$3
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:
`js
require("dotenv-mono").load();
`
###### Example
`js
require("dotenv-mono").load();
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
/ config options here /
};
module.exports = nextConfig;
`
$3
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:
`js
const dotenv = require("dotenv-mono").load();
const config = {
/ config options here /
env: (config) => {
return {
...config,
...dotenv.env,
};
},
};
module.exports = config;
`
π» Usage
$3
Simple methods to export environment variables from the dotenv into the working process.
Here are several potential implementation approaches based on your preferences.
`js
// Inline
require("dotenv-mono").load(/ config /);
// Using the function
const {dotenvLoad} = require("dotenv-mono");
dotenvLoad(/ config /);
// Using import
import {dotenvLoad} from "dotenv-mono";
const dotenv = dotenvLoad(); // Dotenv instance
// Using the class
const {Dotenv} = require("dotenv-mono");
const dotenv = new Dotenv(/ config /);
dotenv.load();
`
#### Having the dotenv output
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.
`js
// Inline
const output = require("dotenv-mono").config(/ config /);
// Using the function
const {dotenvConfig} = require("dotenv-mono");
const output = dotenvConfig(/ config /);
`
$3
`js
// Use .dotenv.server or .dotenv.server.local, etc...
load({extension: "server"});
`
$3
`js
// You can specify the file path
load({path: "../../configs/.env"});
`
$3
`js
load({expand: false});
`
$3
`js
// Suppress console output from dotenv
load({quiet: false});
`
$3
`js
load({defaults: ".env.def"});
`
$3
`js
// If .dotenv.overwrite is present use it with max priority
load({
priorities: {
".env.overwrite": 100,
},
});
`
$3
`js
const dotenv = require("dotenv-mono").load();
dotenv.save({"MY_ENV_1": "enjoy"});
// Without loading into the working process
const {Dotenv} = require("dotenv-mono");
const dotenv = new Dotenv();
dotenv.loadFile(); // Skip loading into the process
dotenv.save({
"MY_ENV_1": "enjoy",
"MY_ENV_2": "'enjoy quotes'",
"MY_ENV_3": 999,
});
`
$3
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.
`bash
$ node -r dotenv-mono/load your_script.js
`
The configuration options below are supported as command line arguments in the format dotenv_config_