cogenv
An efficient and flexible JavaScript library to manage environment variables
📦 Installation
# For npm npm install --save cogenv# For yarn yarn add cogenv
🚀 Get Started
Create an .env
file in the root directory of your project. Add environment-specific variables on new lines in the form of NAME=VALUE. For example, NAME=VALUE:
APP_NAME=CogenvAPP_PORT:string=3000APP_PORT_NUMBER:number=3000APP_URL=http://localhost:${APP_PORT} # For Database DB->dialect=mysqlDB->port=336DB->port_string:string=336DB->localhost=localhostDB->user=rootDB->password=cogenv_passwordDB->logging=trueDB->database=cogenv_databaseDB->URL=http://localhost:${DB.port}
cogenv
will magically transform into the following
As soon as possible in your application, require and configure the cogenv.
// Javascriptrequire'cogenv'.Config; // ES6+ / Typescript;// InitializingConfig;
cogenv now has the keys and values you defined in your cog.env
file.
; env'DB';// orcog.env.DB;
🎨 Typed
cogenv
integrates typing to convert your variables to different types from the .env
file
String
Boolean
Number
# This will return as type number => 3000 APP_NUMBER=3000# This will return of type string => "3000" APP_NUMBER:string=3000 # boolean type => true APP_LOG=yes# boolean type => "yes" APP_LOG:string=yes# boolean type => false APP_LOG=false# string type => "false" APP_LOG:string=false# string type => "true" APP_LOG="true"# boolean type => true APP_LOG:boolean="true" # type number => 8080 APP_PORT=8080# type string => "8080" APP_PORT:string=8080
env
🎉 Method the env method allows you to obtain the environment variables; this method uses the dotfast package and its functionalities are available in this method
;env'APP_NAME'; //=> Cogenvenv'APP_URL'; //=> http://localhost:3000
to obtain from the objects is as follows
env'DB.dialect'; //=> mysqlenv'DB.port'; //=> 336env'DB.logging'; //=> true
from dotfast, to return some data I need, for example
- I need the application port and the database name
this will return an object
env;/*{ PORT: 3000, DB_NAME: "cogenv_database"}*/
let's try an array
env;/*[ 3000, "cogenv_database"]*/
Options
- Path: You may specify a custom path if your file containing environment variables is located elsewhere.
- default:
path.resolve(process.cwd(), '.env')
Config;
- Encoding: You may specify the encoding of your file containing environment variables.
- default:
utf8
Config;
- Logging: you can manipulate the messages on the console
- default:
true
Config;
- InterpolatePrefix: allows you to customize the interpolation prefix
- default:
$
Config;
- TypedOptions: allows you to add typing options, such as mode (
customized
,auto
) - default:
{mode: "auto"}
Config;
Parse
The engine that analyzes the content of your file containing the environment variables is available for use. It accepts a string or buffer and returns an object with the analyzed keys and values.
;;; // will return an objectconsole.logtypeof config, config; // object { BASIC : 'basic' }
⭐ Support for
cogenv
is an open source project licensed by MIT. You can grow thanks to the sponsors and the support of the amazing sponsors. If you want to join them, contact me here.
🎩 Stay in touch
- Github @yonicalsin
- Twitter @yonicalsin
- Instagram @yoni_calsin
- Medium @yonicalsin
Contributors
Thanks to the wonderful people who collaborate with me !
📜 License
cogenv
under License MIT.