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

override.env

v1.1.1

Published

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

Downloads

3

Readme

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