Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork10
Parse dotenv files for Boolean, Array, and Number variable types, built for Lad
License
ladjs/dotenv-parse-variables
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Parse dotenv files for
Boolean,Array, andNumbervariable types, built forLad andForward Email.
npm:
npm install dotenv-parse-variables
yarn:
yarn add dotenv-parse-variables
Imagine you have a configuration file at.env with the following:
FOO=barBAZ=2BEEP=falseBOOP=some,thing,that,goes,wow# note how we use an asterisk here to turn off the parsing for this variableBLEEP=false*# note how we use an asterisk in the array to turn off parsing for an array key valuePING=ping,true*,2,100# note a string between bacticks won't be parsedPONG=`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:
{// StringFOO:'bar',// NumberBAZ:2,// BooleanBEEP:false,// ArrayBOOP:['some','thing','that','goes','wow'],// NOTE: this was not parsed due to the * asterisk override aboveBLEEP:'false',// NOTE: only the "true*" above was opted out through the use of an asteriskPING:['ping','true',2,100],// NOTE: this was not parsed because the string was between bacticksPONG:'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 theString 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 aString 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.
This package works well withdotenv, however we also recommend to usedotenv-extended anddotenv-expand as we do inLad. You could also simply just useLad or@ladjs/env specifically.
Example with
dotenv:
constdotenv=require('dotenv');constdotenvParseVariables=require('dotenv-parse-variables');letenv=dotenv.config({})if(env.error)throwenv.error;env=dotenvParseVariables(env.parsed);console.log(env);
Example with
dotenv-extended(which supports a well-defined.envfile) anddotenv-expand(which supports variable interpolation):
constdotenvExtended=require('dotenv-extended');constdotenvMustache=require('dotenv-mustache');constdotenvParseVariables=require('dotenv-parse-variables');letenv=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 usegetenv (but it requires more work).
A second argument can be provided todotenvParseVariables with an object of options.
The defaults are listed below:
assignToProcessEnv(Boolean) - defaults totrue, whether or not to assign the parsed values toprocess.envoverrideProcessEnv(Boolean) - defaults tofalse, whether or not to override existing values inprocess.envignoreFunctions(Boolean) - defaults totrue, whether or not to ignore functions in the parsed values returned
| Name | Website |
|---|---|
| Nick Baugh | http://niftylettuce.com |
MIT © Nick Baugh
About
Parse dotenv files for Boolean, Array, and Number variable types, built for Lad
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors6
Uh oh!
There was an error while loading.Please reload this page.