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

v0.0.2

Published

Tools to improve use of dotenv. Configure .env path, cast variables to primatives, and decode JSON

Downloads

27

Readme

dotenv-tools


Dotenv-tools is a zero-dependency plugin for dotenv, adding some small, commonly needed tools. There are other dotenv plugins which achieve some of the tasks here, but I felt there was too much going on to achieve a very simple goal; load the environment variables they way I'd expect them to load.

Node's process.env can only store strings, so if using the castVars() function, store the result to a local and/or global variable, and reference it instead of process.env directly. In the example below, I use GLOBAL.env and env for full coverage.

Features include:

  • Specify a file other than .env to load using an environment variable or command line argument.
  • Change the default .env file that dotenv loads.
  • Run projects outside of the project path, and still have dotenv pick up the .env file local to the project source.
  • Returns an object with the newly casted environment variables for you to write to local or global variable.
  • Read the values of the .env file, and automatically cast them as boolean, numeric, or objects.
  • Option to disable casting of specific primitive types.
  • Option to skip casting by ending string with a special character. By default, *.
  • Only use the tools you want.

Examples of running from command line using alternate dotenv file:

# Single execution defining dotenv file via command line argument
node myapp.js --config=.env_alternate

# Single execution defining dotenv file via environment variable:
DOTENV_CONFIG=.env_alternate node myapp.js

# Define dotenv file once via environment variable, and have it persist for the duration of your session:
export DOTENV_CONFIG=.env_alternate
node myapp.js

Example .env with castable variables:

WILL_BE_NUMERIC_A=123
WILL_BE_NUMERIC_B=123.456
WILL_BE_STRING_A=abc
WILL_BE_STRING_B='def'
WILL_BE_STRING_C="ghi"
WILL_BE_STRING_D=true*
WILL_BE_STRING_E=1234*
WILL_BE_STRING_F=["a","b","c"]*
WILL_BE_STRING_G={"j": 1, "k": 2, "l": 3}*
WILL_BE_STRING_H='{"debug":"on","window":{"title":"Sample Widget","name":"main_window","dimentions":[500,500]}}*'
WILL_BE_BOOLEAN_A=true
WILL_BE_BOOLEAN_B=false
WILL_BE_BOOLEAN_C='true'
WILL_BE_BOOLEAN_D='false'
WILL_BE_OBJECT_A=[5,6,7]
WILL_BE_OBJECT_B=["a","b","c"]
WILL_BE_OBJECT_C='["d","e","f"]'
WILL_BE_OBJECT_D='[ "g" , "h" , "i" ]'
WILL_BE_OBJECT_E={"j": 1, "k": 2, "l": 3}
WILL_BE_OBJECT_F={"debug":"on","window":{"title":"Sample Widget","name":"main_window","dimentions":[500,500]}}
WILL_BE_OBJECT_G='{"debug":"on","window":{"title":"Sample Widget","name":"main_window","dimentions":[500,500]}}'

Example usage, using default setup:

// Require dotenv-tools
var dotEnvTools = require('dotenv-tools');

// Create dotenv config object using default dotenv-tools settings
var dotenvCfg = dotEnvTools.getDotEnvConfig();

// Start dotenv with created config object
require('dotenv').config(dotenvCfg);

// Cast process.env variables to likely natives, and save result as both local var env and global.env
var env = GLOBAL.env = dotEnvTools.castVars();

Example usage, using custom setup:

// Require dotenv-tools
var dotEnvTools = require('dotenv-tools');

// Configure dotenv-tools
dotEnvTools.config(
  envFileName: '.env_file',
  configKeyArg: 'configfile',
  castJson: false
);

// Create dotenv config object passing in addional config for returning dotenv object
var dotenvCfg = dotEnvTools.getDotEnvConfig({
  encoding: 'base64'
});

// Start dotenv with created config object
require('dotenv').config(dotenvCfg);

// Cast process.env variables to likely natives, and save result as both local var env and global.env
var env = GLOBAL.env = dotEnvTools.castVars();

Config options, and their defaults:

// Debug casting
debugCasting: false

// Default file name
envFileName: '.env'

// Environment variable to look for to override default file name
configKeyEnv: 'DOTENV_CONFIG'

// Command line argument to look for to override default file name
configKeyArg: 'config'

// Character to look for, signaling to skip cast attempt
passthroughChar: '*'

// Exit if the .env file is not found
requireFile: true

// Try casting strings to numbers
castNumbers: true

// Try casting strings to booleans
castBooleans: true

// Try casting JSON strings to objects. Failure to cast, value remains sting
castJson: true