Socket
Socket
Sign inDemoInstall

@augu/dotenv

Package Overview
Dependencies
1
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @augu/dotenv

🌹 Lightweight and type-safe environment variable parser


Version published
Weekly downloads
7
Maintainers
1
Install size
55.9 kB
Created
Weekly downloads
 

Readme

Source

@augu/dotenv

:rose: Lightweight and type-safe environment variable parser

Usage

const { parse } = require('@augu/dotenv');

parse({
  file: '.env', // the current directory gets added if it's just `.env`
  populate: false, // If we should return the values or populate "process.env"
  readers: [], // Classes of all the custom type readers to use
  schema: {
    // Define a custom schema (if wanted)
  }
});

Type Readers

This library supports adding custom type readers to add more type-safety and validation when parsing. To create one, just make a file myReader.js (or any file name you want) and put in the following code:

Please note that Type Readers are only supported in the Schema options (options.schema), it'll just return as a String if you don't use a proper schema for validation.

const { TypeReader } = require('@augu/dotenv');

module.exports = class MyTypeReader extends TypeReader {
  constructor() {
    super('id');
  }

  validate(value) {
    // validate this `value`
  }

  parse(value) {
    // parse this `value`
  }
};

then you add it to the parse function like so:

const MyTypeReader = require('./path/to/reader');
const { parse } = require('@augu/dotenv');

parse({
  file: '.env',
  readers: [MyTypeReader],
  schema: {
    VALUE: {
      type: 'id' // replace 'id' with your type reader's exact ID
    }
  }
});

Comments

This library supports a way to comment .env files without being parsed! All you need to do is add a # at the start of the pairing and this library will skip it!

.env

# A=B
C=D

index.js

const parse = require('@augu/dotenv');

const result = parse({
  file: '.env', // the current directory gets added if it's just `.env`
  populate: false, // If we should return the values or populate "process.env"
  readers: [], // Classes of all the custom type readers to use
  schema: {
    // Define a custom schema (if wanted)
  }
});
console.log(result); // { c: 'D' }

Does this follow the dotenv spec?

Yes and no. I made this library to show that I wanted to use this for a long time moving from JSON, so I added Array support due to it not being supported in dotenv. If you don't use the Array support that this library adds, then you are following it.

TL;DR - I don't follow spec rules, I like to add my own twists to the libraries I push out.

But why add it if it does support it, but in different ways?

It's just, sometimes you get too lazy to do something like:

const idk = process.env.SOMETHING_ARRAY;
idk?.split(', '); // optional chaining is only allowed in Node v14

because you would have to see if SOMETHING_ARRAY exists and I don't want to do it myself.

I mean, Schemas aren't apart of the .env spec but it's something to validate the file if you don't want the hassle to do if (typeof process.env.IS_THIS_SOMETHING === 'string') and other stuff, it's just convinent out of the box.

License

@augu/dotenv is released under the MIT License. Read here for more information.

FAQs

Last updated on 26 Feb 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc