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

read-env-file

v2.0.3-0

Published

Utilities for reading environment variable names & values from files in ".env" format

Downloads

32

Readme

read-env-file

CircleCI npm: version types

Utilities for reading environment variable names & values from files in ".env" format

  ✔ Reads key/value pairs
  ✔ Handles commented & empty lines
  ✔ Allows spaces in quoted values
  ✔ Allows assignment operators in quoted values
  ✔ Reads from "./.env" when no path arg supplied
  ✔ Merges the results of multiple input files

  ✔ Invalid Format (missing key)
  ✔ Invalid Format (missing value)
  ✔ Invalid Format (missing assignment)
  ✔ Invalid Format (space in key)
  ✔ Invalid Format (space in unquoted value)
  ✔ Invalid Format (multiple assignment operators)
  ✔ Invalid Format Error contains accurate line number

Installation

yarn add read-env-file
# or
npm i read-env-file

Usage

Typescript-friendly (type declarations included)

# path/to/.env
key1=foo
key2=bar
# path/.env
key1=foobar
key3=baz
// path/to/dir/index.js
const { readSingle, readMultiple } = require('read-env-file');

readSingle(); // Attempts to read from ./.env

readSingle('path/to/.env');
// Promise<{key1: 'foo', key2: 'bar'}>

readSingle.sync('path/to/.env');
// {key1: 'foo', key2: 'bar'}

readMultiple(['path/to/.env', 'path/.env']);
// Promise<{key1: 'foobar', key2: 'bar', key3: 'baz'}>

readMultiple.sync(['path/to/.env', 'path/.env']);
// {key1: 'foobar', key2: 'bar', key3: 'baz'}

File Formatting

Valid

  • comments and blank lines are ignored
  • values quoted (with ', ", or backtick) to include spaces and = character
  • spaces around keys and values are ignored

Examples:

key=value
# comment starts with "#" (ignored)

# whitespace is trimmed from keys and values
key = value
key =value
key= value

# whitespace in quoted values preserved
key="value with spaces'

# Assignment operators in quoted values preserved
key="value=abc"

Invalid (throws Error)

Errors are informative and specify the cause, file, and line number:

Invalid file format (multiple assignment operators) at /some/path/.env:12

The following conditions cause an invalid format error:

| Cause | Error Mssage | | ----------------------------- | ------------------------------- | | missing key | key undefined | | missing value | value undefined | | missing assignment | value undefined | | space in key | invalid spacing | | space in unquoted value | invalid spacing | | multiple assignment operators | multiple assignment operators |

Examples:

# missing key
=value

# missing value
key=

# missing assignment
keyvalue

# space in key
k e y = value

# space in unquoted value
key=value and words

# multiple assignment operators
key=value=invalid

Dependencies

None

License

MIT