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-parse-variables

v2.0.0

Published

Parse dotenv files for Boolean, Array, and Number variable types, built for CrocodileJS

Downloads

495,714

Readme

dotenv-parse-variables

build status code coverage code style styled with prettier made with lass license

Parse dotenv files for Boolean, Array, and Number variable types, built for Lad and Forward Email.

Table of Contents

Install

npm:

npm install dotenv-parse-variables

yarn:

yarn add dotenv-parse-variables

Example

Imagine you have a configuration file at .env with the following:

FOO=bar
BAZ=2
BEEP=false
BOOP=some,thing,that,goes,wow
# note how we use an asterisk here to turn off the parsing for this variable
BLEEP=false*
# note how we use an asterisk in the array to turn off parsing for an array key value
PING=ping,true*,2,100
# note a string between bacticks won't be parsed
PONG=`some,thing,that,goes,wow`

After using this plugin, the environment variables are parsed to their proper types.

To test it out, simply log the returned object in your console:

console.log(env);

And you'll see that it outputs the properly parsed variable types:

{
  // String
  FOO: 'bar',
  // Number
  BAZ: 2,
  // Boolean
  BEEP: false,
  // Array
  BOOP: [ 'some', 'thing', 'that', 'goes', 'wow' ],
  // NOTE: this was not parsed due to the * asterisk override above
  BLEEP: 'false',
  // NOTE: only the "true*" above was opted out through the use of an asterisk
  PING: [ 'ping', 'true', 2, 100 ],
  // NOTE: this was not parsed because the string was between bacticks
  PONG: 'some,thing,that,goes,wow'
}

If your configuration line ends in * it will not be parsed by this package, which allows you to keep values as the String variable type if needed. Also when you encapsulate a value between bacticks e.g. `value`, the value won't be parsed and it will return as a String variable. This can be used in situations where you for example have a , inside your string and it should not be parsed as an array.

Usage

This package works well with dotenv, however we also recommend to use dotenv-extended and dotenv-expand as we do in Lad. You could also simply just use Lad or @ladjs/env specifically.

Example with dotenv:

const dotenv = require('dotenv');
const dotenvParseVariables = require('dotenv-parse-variables');

let env = dotenv.config({})
if (env.error) throw env.error;
env = dotenvParseVariables(env.parsed);

console.log(env);

Example with dotenv-extended (which supports a well-defined .env file) and dotenv-expand (which supports variable interpolation):

const dotenvExtended = require('dotenv-extended');
const dotenvMustache = require('dotenv-mustache');
const dotenvParseVariables = require('dotenv-parse-variables');

let env = dotenvExtended.load({
  silent: false,
  errorOnMissing: true,
  errorOnExtra: true
});
env = dotenvMustache(env);
env = dotenvParseVariables(env);

console.log(env);

If you don't want to use this package to parse variable types, you could also use getenv (but it requires more work).

Options

A second argument can be provided to dotenvParseVariables with an object of options.

The defaults are listed below:

  • assignToProcessEnv (Boolean) - defaults to true, whether or not to assign the parsed values to process.env
  • overrideProcessEnv (Boolean) - defaults to false, whether or not to override existing values in process.env
  • ignoreFunctions (Boolean) - defaults to true, whether or not to ignore functions in the parsed values returned

Contributors

| Name | Website | | -------------- | ------------------------- | | Nick Baugh | http://niftylettuce.com |

License

MIT © Nick Baugh