# dotenv-multi

> Load multiple environment variables using dotenv

Latest version **1.0.0** (published 2020-06-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install dotenv-multi
pnpm add dotenv-multi
yarn add dotenv-multi
bun add dotenv-multi
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2020-06-26 |
| First published | 2020-06-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 7.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | render |
| Maintainers | zjcdaniel |
| Keywords | dotenv, env, .env, environment, variables, config, settings, multiple, mode |

## Links

- npm: https://www.npmjs.com/package/dotenv-multi
- Repository: https://github.com/zjcrender/dotenv-multi
- Homepage: https://github.com/zjcrender/dotenv-multi#readme
- Issues: https://github.com/zjcrender/dotenv-multi/issues
- npm.io page: https://npm.io/package/dotenv-multi

## Dependencies (2)

- [dotenv](https://npm.io/package/dotenv.md) ^8.2.0
- [dotenv-expand](https://npm.io/package/dotenv-expand.md) ^5.1.0

## Alternatives

- [replicas-cli](https://npm.io/package/replicas-cli.md) — 3.0K weekly downloads
- [env-contract](https://npm.io/package/env-contract.md) — 133 weekly downloads
- [@openveo/api](https://npm.io/package/@openveo/api.md) — 61 weekly downloads
- [@ryniaubenpm2/cumque-error-reiciendis](https://npm.io/package/@ryniaubenpm2/cumque-error-reiciendis.md) — 54 weekly downloads
- [ts-global-type-extra](https://npm.io/package/ts-global-type-extra.md) — 11 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2020-06-26

## README

# Welcome to dotenv-multi 👋
![Version](https://img.shields.io/badge/version-1.0.0-blue.svg?cacheSeconds=2592000)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](#)

English | [中文文档](./README-zh.md)

`dotenv-multi` Use [dotenv](https://github.com/motdotla/dotenv) to parse and load variables from multiple env files to process.env, and distinguish between different modes.

## Install

```sh
// with npm
npm install dotenv-multi

// with yarn
yarn add dotenv-multi
```

## Usage
```javascript
const dotenvMulti = require('dotenv-multi');
dotenvMulti.loadEnv({ dir: 'path/to/envFileDir', mode: 'myMode' });
```

## API

### loadEnv(options: [LoadOptions](#LoadOptions)): [Result](#Result)
loadEnv will parse and merge the following files into `process.env` in the `dir` folder.
- .env
- .env.local
- .env.[mode]
- .env.[mode].local

Note：
1. Priority of the above files from low to high
2. The file will be skipped if the file does not exist
3. Will not overwrite the original value in `process.env`
4. The method used to merge the variables in the file is "Object.assign".

## Example
Assume that there are some env files in the `env/` folder
```
// .env
foo=foooooo
URL=api.default.com
endpoint=path/to/endpoint

// .env.dev
URL=api.dev.com

// .env.local
URL=api.dev-local.com
e1=some-local-var

//.env.stg
URL=api.stg.com

//.env.prd
bar=barrrrr
URL=api.prd.com

//.env.prd.local
URL=api.prd.com
enpoint=/another/path/to/endpoint
proxy=/another
```
  
Suppose we distinguish the modes through `process.env.API`
```javascript
const dotenvMulti = require('dotenv-multi');
dotenvMulti.loadEnv({ dir: './env', mode: process.env.API }); 
```
Start the script in different ways as follows
```shell script
# load .env、.env.local、.env.dev、.env.dev.local
$ cross-env API=dev npm run some-script

# load .env、.env.local、.env.stg、.env.stg.local
$ cross-env API=stg npm run some-script

# load .env、.env.local、.env.prd、.env.prd.local
$ cross-env API=prd npm run some-script
```
Then we can get the corresponding environment variables in the project


## Interface
### LoadOptions
|Key|Type|Default|Required|Description|
|---|---|---|---|---|
|dir|string|`process.cwd()`|N|Directory path where `env` files are stored|
|mode|string|process.env.NODE_ENV|N|current mode|

### Result: ParseSuccess | ParseError
```typescript
interface ParseSuccess {
  parsed: { [props: string]: string };
  error: null;
}

interface ParseError {
  error: Error;
  parsed: null;
}
```


## Author

👤 **render**

* Website: http://render.ink
* Github: [@zjcrender](https://github.com/zjcrender)

## Show your support

Give a ⭐️ if this project helped you!

---
_Source: https://npm.io/package/dotenv-multi · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
