• Stars
    star
    122
  • Rank 292,031 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

More Repositories

1

superagent

Ajax for Node.js and browsers (JS HTTP client). Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
JavaScript
16,475
star
2

supertest

🕷 Super-agent driven library for testing node.js HTTP servers using a fluent API. Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
JavaScript
13,355
star
3

lad

Node.js framework made by a former @expressjs TC and @koajs team member. Built for @forwardemail, @spamscanner, @breejs, @cabinjs, and @lassjs.
JavaScript
2,250
star
4

react-native-loading-spinner-overlay

💈 React Native loading spinner overlay
JavaScript
1,583
star
5

frisbee

🐕 Modern fetch-based alternative to axios/superagent/request. Great for React Native.
JavaScript
1,123
star
6

express-cdn

☁️ Node module for delivering optimized, minified, mangled, gzipped assets with Express and Amazon's CDN (S3/CloudFront)
JavaScript
662
star
7

react-native-phone-verification

The best React Native example for phone verification (an alternative to Twitter Digits).
JavaScript
374
star
8

frappe

🆓 Remotely shake your Android devices (including emulators) using a menubar applet and the hotkey ⌘+SHIFT+R
JavaScript
215
star
9

i18n-locales

List of locales for i18n
JavaScript
72
star
10

graceful

Gracefully exit HTTP servers (Express/Koa/Fastify/etc), databases (Mongo/Mongoose), Redis clients, Bree job schedulers, and custom handlers.
JavaScript
66
star
11

koa-better-error-handler

A better error-handler for Lad and Koa. Makes `ctx.throw` awesome (best used with koa-404-handler)
JavaScript
52
star
12

chalkline

💄 Draw a big chalkline in your terminal! Great for debugging and reading your log output locally!
JavaScript
35
star
13

cache-pug-templates

Cache Pug templates for Lad/Koa/Express/Connect with Redis
JavaScript
26
star
14

mongoose-slug-plugin

Slugs for Mongoose with history and i18n support (uses speakingurl by default, but you can use any slug library such as limax, slugify, mollusc, or slugme)
JavaScript
22
star
15

express-cachebuster

Provides cross-browser version-control/cache-busting as a dynamic view helper in express.
JavaScript
22
star
16

gulp-jade-usemin

Gulp plugin for running usemin on Jade files
JavaScript
21
star
17

max-listeners-exceeded-warning

Debug and detect "MaxListenersExceededWarning: Possible EventEmitter memory leak detected warnings"
JavaScript
19
star
18

country-language

Query any country's spoken languages or countries where a language is spoken.
JavaScript
12
star
19

mandarin

Automatic i18n phrase translation using Google Translate
JavaScript
12
star
20

i18n

i18n wrapper and Koa middleware for Lad
JavaScript
10
star
21

naivebayes

A ladjs naivebayes package forked from `https://github.com/surmon-china/naivebayes`
JavaScript
10
star
22

web

Web server for Lad
JavaScript
9
star
23

koa-better-flash

Flash message middleware for Koa and Passport
JavaScript
9
star
24

env

Environment configuration loader for Lad
JavaScript
8
star
25

mongoose-common-plugin

Common plugin for Mongoose with standard schema fields and localization support
JavaScript
8
star
26

check-chai

✅ Adds chai helper function `chai.check` for asynchronous testing with multiple expect or assert statements
JavaScript
8
star
27

koa-404-handler

404 handler for Lad and Koa (best used with koa-better-error-handler)
JavaScript
7
star
28

mongoose-validation-error-transform

Automatically transform Mongoose validation error message(s) to a humanized and readable format
JavaScript
7
star
29

express-cdn-cloudfront

Add-on module for express-cdn to provide Amazon CloudFront integration with Amazon S3.
7
star
30

passport

Passport for Lad
JavaScript
6
star
31

lad.sh

Demo for Lad - The Best Node.js Framework
JavaScript
6
star
32

remark-preset-github

GitHub markdown and prose style
JavaScript
6
star
33

dayjs-with-plugins

Day.js with all plugins and locales added out of the box, no need to use dayjs.extend!
JavaScript
6
star
34

is-string-and-not-blank

3x as fast as is-whitespace and whitespace-regex thanks to is-string-blank. This package is a simple function that accepts an argument and returns true if it is a string AND it is not blank.
JavaScript
5
star
35

redis

Redis for Lad
JavaScript
5
star
36

koa-meta

SEO <title> and <meta name="description"> middleware for Koa and Lad
JavaScript
5
star
37

koa-cache-responses

Caching middleware for Koa using koa-cash and route pattern-based matching with path-to-regexp. Made for Lad.
JavaScript
5
star
38

express-cdn-cloudfiles

Add-on module for express-cdn to provide Rackspace CloudFiles integration with built-in Akamai CDN delivery.
5
star
39

lookerupper

✨ :octocat: Copy to your clipboard a package's name and use `CmdOrCtrl+Shift+L` to lookup its documentation on GitHub
JavaScript
5
star
40

express-redirect-loop

Prevent redirect loops with sessions since HTTP referrer header is unreliable
JavaScript
4
star
41

get-paths

Helper function to get an absolute path for a template engine view
JavaScript
4
star
42

spdy-or-http2

Node.js ponyfill for HTTP/2 support (uses native http2 module or falls back to spdy)
JavaScript
4
star
43

proxy

Proxy for Lad
JavaScript
4
star
44

api

API server for Lad
JavaScript
4
star
45

koa-views-render

Simple render(page, locals) middleware for Koa and Lad (uses koa-views)
JavaScript
4
star
46

koa-better-timeout

Response timeout middleware for Koa and Lad (uses Boom by default)
JavaScript
4
star
47

express-jade

Express middleware to compile client-side Jade templates as template functions in the `window.jade` namespace.
JavaScript
4
star
48

koa-redis-ratelimit

*DOES NOT WORK DUE TO `ratelimiter`, PLEASE USE https://github.com/scttcper/koa-simple-ratelimit INSTEAD* Rate limiting middleware backed by Redis for Koa v2+, built for 🐊 CrocodileJS
JavaScript
3
star
49

mongoose

Mongoose helper for Lad
JavaScript
3
star
50

koa-redirect-loop

Prevent redirect loops with sessions since HTTP referrer header is unreliable and ensures sessions are saved upon redirect
JavaScript
3
star
51

store-ip-address

Stores a user's IP address in the background for Lad
JavaScript
3
star
52

message-headers

Automatically updated list of RFC HTTP permanent and provisional headers from IANA (https://www.iana.org/assignments/message-headers/message-headers.xhtml)
JavaScript
3
star
53

mongoose-omit-common-fields

Array of common fields to emit for Mongoose toObject/toJSON (helpful for security)
JavaScript
3
star
54

mongoose-model-agenda

Mongoose model for Agenda
JavaScript
3
star
55

express-cdn-cloudflare

Add-on module for express-cdn to provide CloudFlare CDN integration with Amazon S3.
3
star
56

koa-better-static

Static file serving middleware for Koa. Forked from an inactive project and maintained for Lad.
JavaScript
3
star
57

koa-manifest-rev

Dynamically load assets into your views from your `rev-manifest.json` manifest revision file.
JavaScript
3
star
58

stop-agenda

Gracefully stop Agenda and cancel recurring jobs
JavaScript
2
star
59

assets

Assets for Lad
JavaScript
2
star
60

policies

Policies helper for Lad
JavaScript
2
star
61

shared-config

Shared configuration for Lad's API and Web servers
JavaScript
2
star
62

manifest-rev

Dynamically load assets into your views, emails, etc. from your `rev-manifest.json` manifest revision file.
JavaScript
2
star
63

gulp-envify

Gulp plugin for envify without browserify (maintained fork of https://github.com/tomashanacek/gulp-envify)
JavaScript
2
star
64

preserve-qs

Preserve querystrings during redirect and creating new URLs for Node.js and browser environments (supports Lad, Koa, Express, and Connect)
JavaScript
2
star
65

juice-resources-promise

Simple helper function to convert juice.juiceResources into a Promise
JavaScript
2
star
66

gulp-haschanged-deps-async

Deep dependency recursive modified time comparison to ensure your files only re-compile when needed
JavaScript
2
star
67

pick-original

Transform an Object that was transformed to return only the original properties recursively picked if they are not undefined.
JavaScript
1
star
68

koa-cors-gate

JavaScript
1
star
69

store-sessions

Store/manage user sessions in the background for Lad
JavaScript
1
star
70

mongoose-error-messages

Better error messages for Mongoose, built for Lad
JavaScript
1
star
71

lad-template

sao template for lad
JavaScript
1
star
72

state-helper

State helper for Lad
JavaScript
1
star
73

agenda

Agenda for Lad
JavaScript
1
star
74

bull

Bull for Lad
JavaScript
1
star
75

browserslist-config

Browserslist config for Lad
JavaScript
1
star