Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dotenv-mono

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dotenv-mono

This is a package that permit to load a dotenv even from a children applications or packages of a monorepo. It contains also some additionals features like manipulations and save of the changes on the dotenv file.

  • 1.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
22K
increased by20.36%
Maintainers
1
Weekly downloads
 
Created
Source

Dotenv mono

If this project has helped you out, please support us with a star 🌟


NPM version js-prettier-style

📘 Description

What is this?

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.

The plugin dotenv-expand is enabled by default.

Structure Example
├── .env
├── packages
│   ├── ui-library
│   ├── other-library
├── apps
│   ├── web
│   ├── 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.

PriorityFilename
75.env.$(NODE_ENV).local
50.env.local
25.env.$(NODE_ENV)
1.env
Example

Given the following folder structure with dotenv files:

├── .env
├── .env.production
├── apps
│   ├── .env.development
│   ├── web
│   ├── docs
│   │   ├── .env
│   │   ├── .env.local

Having the following priority order:

PathPriorityDepth
.env12
.env.production252
apps/.env.development251
apps/docs/.env10
apps/docs/.env.local500

Then we will have the following outcome scenarios:

Current working directoryEnvMatch
/development.env
/production.env.production
apps/webdevelopment.env
apps/webdevelopmentapps/.env.development
apps/docsdevelopmentapps/docs/.env.local

📖 Install

Install the library from npm or yarn just running one of the following command lines:

npmyarn
npm install dotenv-mono --saveyarn add dotenv-mono

Install on Next.js

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:

require("dotenv-mono").load();
Example
require("dotenv-mono").load();

/**
 * @type {import('next').NextConfig}
 */
const nextConfig = {
	/* config options here */
};

module.exports = nextConfig;

💻 Usage

Load

Standard
require("dotenv-mono").load();

// or

const {dotenvLoad} = require("dotenv-mono");
dotenvLoad();

// or

import {dotenvLoad} from "dotenv-mono";
dotenvLoad();
Using the class
const {Dotenv} = require("dotenv-mono");
const dotenv = new Dotenv();
dotenv.load();
Having the output

If you want to have back directly the output like dotenv package.

require("dotenv-mono").config();

Load file with extension

// Use .dotenv.server or .dotenv.server.local, etc...
dotenvLoad({extension: "server"});

Load specific file

// You can specify the file path
dotenvLoad({path: "../../configs/.env"});

Load without dotenv-expand extension

dotenvLoad({expand: false});

Change priorities

// If .dotenv.overwrite is present use it with max priority
dotenvLoad({
	priorities: {
		".env.overwrite": 100,
	},
});

Make changes

const dotenv = new Dotenv();
dotenv.loadFile(); // Not loading into process
dotenv.save({
	"MY_ENV_1": "enjoy",
	"MY_ENV_2": "'enjoy quotes'",
	"MY_ENV_3": 999,
});

💡 Methods

Config

SettingDescriptionDefault
cwdSpecify the current working directoryprocess.cwd()
debugTurn on/off logging to help debug why certain keys or values are not being set as you expectfalse
depthSpecify the max depth to reach finding up the folder from the children directory4
encodingSpecify the encoding of your file containing environment variablesutf8
expandTurn on/off the dotenv-expand plugintrue
extensionSpecify to load specific dotenv file used only on specific apps/packages (ex. .env.server...)
overrideOverride any environment variables that have already been set on your machine with values from your .env filefalse
pathSpecify a custom path if your file containing environment variables is located elsewhere
prioritiesSpecify the criteria of the filename priority to load as dotenv fileSee Priorities

Methods

Load

It will read your .env file following the criteria, parse the contents, assign it to process.env.

public load(loadOnProcess: boolean): Dotenv;
LoadFile

It will read your .env file following the criteria, parse the contents, ready to be read or changed programmatically.

public loadFile(): Dotenv;
Save

Merge the data on input with the loaded data from load or loadFile, and save the changes on the original dotenv file.

public save(changes: Record<string, any>): Dotenv;
Parse

See the dotenv documentation HERE

public parse<T extends Record<string, any> = Record<string, any>>(src: string | Buffer): T;

🤔 How to contribute

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.


Keywords

FAQs

Package last updated on 18 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc