Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

ladjs/dotenv-parse-variables

build statuscode coveragecode stylestyled with prettiermade with lasslicense

Parse dotenv files forBoolean,Array, andNumber variable types, built forLad andForward 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=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.

Usage

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 withdotenv:

constdotenv=require('dotenv');constdotenvParseVariables=require('dotenv-parse-variables');letenv=dotenv.config({})if(env.error)throwenv.error;env=dotenvParseVariables(env.parsed);console.log(env);

Example withdotenv-extended (which supports a well-defined.env file) 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).

Options

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.env
  • overrideProcessEnv (Boolean) - defaults tofalse, whether or not to override existing values inprocess.env
  • ignoreFunctions (Boolean) - defaults totrue, whether or not to ignore functions in the parsed values returned

Contributors

NameWebsite
Nick Baughhttp://niftylettuce.com

License

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

Stars

Watchers

Forks

Sponsor this project

    Packages

    No packages published

    Contributors6


    [8]ページ先頭

    ©2009-2025 Movatter.jp