1.1.1 • Published 2 years ago

override.env v1.1.1

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

Override.env

Override dotenv configuration using a given .env file. This is a missing piece of the dotenv lib.

Why?

Despite the fact that dotenv package discourages this, it's actually very very useful, especially during development. You can compose .env files that do different things based on what you want, or the environment that the script is running under.

Let's say you have a development .env file that you use for development, and you want to change just the database that tests use. That can be achieved by one of the following solutions:

  • edit the .env file each time, not sustainable (someone else or even yourself will not do this all the time)
  • copy the same file as .env.test and edit that one var (what if you want to change something else, you have maintenance issues to try and keep both files in sync)
  • use commandline to set the var each time you run it, not sustainable

Instead of all this, just create a .env.test file with just the vars that you want to override and use this lib to do the override.

Usage

yarn add override.env

or

pnpm add override.env

or

npm install override.env

Overriding

Carrying over from the above scenario, let's override some config.

The lib just exports a default function as explained in the dotenv documentation, with a little extra salt.

.env file:

#DB CONFIG
HOST=host
USER=root
PASSWORD=password
DB=development
DIALECT=mysql

Load the .env file as you normally do (through dotenv or maybe loaded by your framework), then do the override.

Override .env file:

Create a .env.test file with your override:

DB=testing_db_name

Then do the following (it doesn't matter whether this is done before or after the .env file was loaded into process.env since dotenv will not overwrite existing configs):

// => process.env.DB === 'developement'
import override from 'override.env';

override ('.env.test', 'test');
// => process.env.DB === 'testing_db_name'

API

override ( envFile: string, env?: string )

  • envFile: required - the file to use for the override
  • env: optional - if specified, then the override will only be done if NODE_ENV is the same as its value. If not specified, then the override happens immediately.

CLI

You can also override the override, or prevent the override from the cli. This is useful to temporarily stop the override for a specific command, but not when run normally.

Override working as usual

Override will be done as per above logic:

yarn test

DB will be assigned 'test'

Override the override

What if you want to temporarily set something else just for this run?

SET__: override the override and use what we give it from the CLI:

SET__DB=other yarn test

DB will be assigned 'other' (note the double underscore)

Prevent the override

What if we don't want the override for this run?

NO_: prevent the override and don't do anything with a specific var:

  • anything can be put after the =
NO_DB=_ yarn test

DB will be assigned 'developement' (from the original .env file)

Author

Emmanuel Mahuni

License

MIT