Code Monkey home page Code Monkey logo

dotenv-multi-x's Introduction

Contains the functions of the following libraries

๐Ÿ‘‹ Features

  • Multiple .env file support
  • Command Line support
  • Assign a mode mode

Support multiple .env files and keep the inheritance

File Priority:

  • .local file > not unassigned local
  • .mode file > not unassigned mode

If the mode is 'dev', then the import order is:

  1. .env.dev.local
  2. .env.dev
  3. .env.local
  4. .env
# the local file has higher priority

# in .env file
HOST=127.0.0.1
PORT=3000
# in .env.local file
PORT=3001

# out
{"HOST": "127.0.0.1", "PORT": "3001"}
# the assigned mode file has higher priority

# in .env file
PORT=3000
# in .env.prod file
PORT=80

# mode=prod
# out
{"PORT": "80"}

๐Ÿ’กIf you have used vite, it works the same way.

How to use

npm i dotenv-multi-x
# or
yarn add dotenv-multi-x
import dotenv from 'dotenv-multi-x'
dotenv.init()

console.log(process.env)

or auto initial

// notice, keep it in the top of file.
import dotenv from 'dotenv-multi-x/lib/init'

console.log(process.env)

Commond Line

$ dotenv node ./example/cli.test.js
$ dotenv --mode=dev node ./example/cli.test.js

OR

$ node -r dotenv-multi-x/lib/init.js ./example/cli.test.js
$ node -r dotenv-multi-x/lib/init.js ./example/cli.test.js --mode=dev

Methods

  • init
  • parse
  • getConfig

init

init will get mode from process.env or process.argv, read the .env* files, parse the content, handle the inheritance, and reture an object.

dotenv.init()

parse

Parse the content and return an Object with the parsed keys and values.

dotenv.parse(Buffer.from('PROT=3001'))

getConfig

Accept a mode and read .env* files, and handle the inheritance. return finally result.

Example

# Windows Powershell
$env:mode="dev"
node .\example\index.mjs
# Mac
mode=dev node ./example/index.mjs

# or
node .\example\index.mjs --mode=dev

Suggest

Add .env.local* in your .gitignore file.

Why not dotenv

When you run your code in multiple environments, you may need some different environments variable. But dotenv didn't support multiple .env files.

If you don't use docker or other CI/CD environment variable to instead of .env file, or don't use shell script to replace .env file, the multiple files is the easiest way to make it work.

For example, your server launched on port 3000, but you want to run on 3001 in local device, the .env file will be shared on repos which used git, so you need a .env.local file, this file has higher-priority then .env and it can doesn't share with git.

You can create mutiple .env* files, and use them in different environments as easier as possible.

dotenv-multi-x's People

Contributors

x-ray-s avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.