npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dotenv-multi

v1.0.0

Published

Load multiple environment variables using dotenv

Downloads

260

Readme

Welcome to dotenv-multi 👋

Version License: MIT

English | 中文文档

dotenv-multi Use dotenv to parse and load variables from multiple env files to process.env, and distinguish between different modes.

Install

// with npm
npm install dotenv-multi

// with yarn
yarn add dotenv-multi

Usage

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

API

loadEnv(options: LoadOptions): 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

const dotenvMulti = require('dotenv-multi');
dotenvMulti.loadEnv({ dir: './env', mode: process.env.API }); 

Start the script in different ways as follows

# 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

interface ParseSuccess {
  parsed: { [props: string]: string };
  error: null;
}

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

Author

👤 render

Show your support

Give a ⭐️ if this project helped you!