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

v0.1.1

Published

extend dotenv to support defaults, types and nesting

Downloads

12

Readme

dotenv-schema

extend dotenv to support defaults, types and structure

don't limit your application configuration only to strings!

Note that this module does not modify process.env in any way. It parses process.env and then returns an object that reflects the provided schema. You should call to dotenv.config() to load .env file

Example


configSchemaTest = {
  test1: 'aaa', // simple string value. if there is TEST1 environment variable, it will get that value instead
  test2: 1, // Numbers are parsed from .env
  test3: 1.3, // Floats are parsed too
  test4: false, // if env.TEST4 is 1,true,on... this will be true
  test5: String, // specify type without default value
  test6: Number,
  test7: Boolean,
  test8: Date,
  test9: {
    type: String, // specify type
    default: 'abc', // default value if not found in .env
  },
  test10: {
    type: Number, // specify type
    value: 1, // specify value if not found in .env
  },
  test11: {
    type: String,
    // required: true, // will throw error if env.TEST11 is not defined
  },
  test12: {
    envName: 'OTHER_NAME', // will read value from env.OTHER_NAME instead of TEST12
  },
  nested: { // keys can be nested
    test13: String, // this will evaluate from 'NESTED_TEST13'
    more: {
      test13: { type: Boolean } // = NESTED_MORE_TEST13
    },
  },
  camelCaseConversion: 1, // this will convert to environment variable 'CAMEL_CASE_CONVERSION',
};

const myAppConfig = require('dotenv-schema').config(configSchemaTest);

console.log(myAppConfig);
/*
{ test1: 'aaa',
  test2: 1,
  test3: 1.3,
  test4: false,
  test5: '',
  test6: '',
  test7: false,
  test8:
   'Wed Dec 11 2019 12:31:46 GMT+0100 (Central European Standard Time)',
  test9: 'abc',
  test10: 1,
  test11: '',
  test12: '',
  nested: { test13: '', more: { test13: '' } },
  camelCaseConversion: 1 }
*/
dumpConfig
// dumpConfig does the reverse: it returns all lines with all of the keys defined in schema like .env file would look like
// this is useful for generating .env files

console.log(require('dotenv-schema').config(configSchemaTest));
/*
TEST1=aaa
TEST2=1
TEST3=1.3
TEST4=false
TEST5=
TEST6=
TEST7=false
TEST8=Wed Dec 11 2019 12:42:50 GMT+0100 (Central European Standard Time)
TEST9=abc
TEST10=1
TEST11=
OTHER_NAME=
NESTED_TEST13=
NESTED_MORE_TEST13=
CAMEL_CASE_CONVERSION=1
*/