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-dot

v1.0.1

Published

Transform .env variables into condensed JSON variables for use in nodejs projects.

Downloads

621

Readme

dotenv-dot

Dotenv-dot adds dot-notation variable transformation on top of dotenv. If you find yourself needing to condense environment variables into JSON variables, then dotenv-dot is your tool.

Release Version License

Install

npm install dotenv --save
npm install dotenv-dot --save

Usage

Dot-notation variables are those which contain a dot (.) in their name. Add dot-notation variables to the .env file like any other variable.

MAIL_CONFIG.service=gmail
[email protected]
MAIL_CONFIG.auth.pass=pass1234

Once the dotenv-dot transformer is executed, these variables will be condensed into a new variable named MAIL_CONFIG.

MAIL_CONFIG='{"service":"gmail","auth":{"user":"[email protected]","pass":"pass1234"}}'

Transform process.env variables

As early as possible in your application, require dotenv and dotenv-dot. Then, execute dotenv-dot after dotenv.

const dotenv = require('dotenv');
const dotenvDot = require('dotenv-dot').transform;

const myEnv = dotenv.config();
const results = dotenvDot(); // only updates `process.env`

Transform process.env and the results of dotenv.config()

If you want to update process.env and the output of the dotenv.config(), include the output as a parameter on the dotenv-dot transformer.

const myEnv = dotenv.config();
const results = dotenvDot(myEnv); // updates `process.env` and `myEnv`

Transform variables without affecting process.env

It is possible to use dotenv without adding variables to process.env. Dotenv-dot can also do the same.

const parsedOutput = dotenv.parse(`MAIL_CONFIG.service=gmail
[email protected]
MAIL_CONFIG.auth.pass=pass1234`);

const results = dotenvDot(parsedOutput);

If you already have parsed output you may transform it without using dotenv.

const results = dotenvDot({
  'MAIL_CONFIG.service': 'gmail',
  'MAIL_CONFIG.auth.user': '[email protected]'
  'MAIL_CONFIG.auth.pass': 'pass1234'
});

Options

debug

Default: false

You may turn on logging to help debug why certain keys or values are not being set as expected.

const myEnv = dotenv.config();

require('dotenv-dot').transform(myEnv, {
  debug: process.env.DEBUG
});
ignoreProcessEnv

Default: false

You may want to ignore process.env when transforming the output of the dotenv.config(). When this option is turned on process.env will not be consulted or altered.

const myEnv = dotenv.config();

require('dotenv-dot').transform(myEnv, {
  ignoreProcessEnv: true
});

How do I use dotenv-dot with import?

import * as dotenv from 'dotenv';
import dotenvDot from 'dotenv-dot';

Or, if only intending to access process.env, the two modules should be loading in this order using their auto-imports:

import 'dotenv/config';
import 'dotenv-dot/transform';

More information about the import syntax is available on the dotenv repository.

Note: You may set the debug option using the dotenv debug command line or environment variable. The ignoreProcessEnv option is irrelevant when using the auto-imports.

How do I add arrays to the .env file?

Arrays are added by using numbers to indicate the value's index in the resulting array.

THINGS.0='Was eaten by his others'
THINGS.1='Thing One'
THINGS.3='Wayward Thing Two'