A simple js wrapper around environmental variables, to allow upfront loading, checking, and defaulting of required variables
- Installation
- Load environment from a file
- Read and enforce that a required environment property is present
- Read and default an optional environment property
- Simple getter / setter
- Testing
- Release History
- License
From the terminal, install the package from npm:
npm i env-wrapper
From within your code, import the library:
import env from 'env-wrapper'
Optionally read key-value pairs from a file and insert them into the environment. If a filename is not given, '.env' in the base directory is used. If either the assumed or named file does not exist, no error is issued. An error will be issued if the file exists but and error in encountered. Note that this is a convenience method, and env-wrapper will readily handle any environment variables inserted through other means.
await env.load()
await env.load('development.env')
Example .env file:
PORT=1234
DATABASE_URL=db://localhost
The original intent of this library was to simply ensure fail-fast behavior if a required environment variable had not been specified, so that we know when the application loads instead of 3am next Tuesday when actually uses it.
const databaseUrl = env.require('DATABASE_URL')
If the requested environment property cannot be found, and a default value is specified, that value will be inserted into the environment, and then returned as the requested value. Because the environment can be altered by this form, this can also be used as a shortcut for conditionally priming the environment for later use.
const port = env.require('PORT', 3000)
The library also provides a simple getter and setter that can be used for unconditionally inserting a value into the environment, or retrieving a value from the environment. Unlike require above, get will either return the found value, or undefined if a value is not found.
env.set('my-key', 'abc')
const key = env.get('my-key')
Any line where the first non-space character is '#' will be ignored. This allows commenting for clarity, or temporarily removing a line from effect with out actually removing it.
Example .env file:
# Credentials
USERNAME=public
PASSWORD*=private
Adding an object that sets debug to true will print any variables that are loaded to the console.
await env.load({ debug: true })
await env.load('development.env', { debug: true })
Any key names that end with a *
will have their value replaced with (secret)
in the console.
Example .env file:
USERNAME=public
PASSWORD*=private
There are several Jasmine-based unit tests that can be run from the terminal if desired:
npm test
Version | Changes |
---|---|
2.2.1 | Update docs for comments (smh) |
2.2.0 | Added basic support for comments |
2.1.1 | Expanded debug information |
2.1.0 | Added debug option for load function |
2.0.0 | Updated to be more ES6 friendly; load is now async |
1.0.7 | Documentation revamp |
1.0.6 | Fix to handle .env file values with embedded '=' symbols |
1.0.5 | Changed load to sync operation; Fix to path calculations |
1.0.4 | Added load method; moved tests to Jasmine |
1.0.3 | Require method returns value |
1.0.2 | Fix for error message unit test |
1.0.1 | Added set method; fixed package.json NPM error |
1.0.0 | Initial Release |
This work is released under the MIT license - see the LICENSE file for details