Introduction
dotenv-Manipulator :
- Loads environment variables from a
.env
file toprocess.env
- Adds, updates, or removes variables from both your .env file and process.env at runtime
- Has no dependency (you don't need the
dotenv
package)
Installation
npm i dotenv-manipulator
Import/require
const Manipulator = // or
Setup and start
Manipulator constructor has 3 optional arguments:
envPath throwable encoding
- Path to
.env
, default isprocess.cwd()
. - If set to
true
functions throw errors whenever there's an incorrect input. - Specify file encoding, default is
utf8
.
// basic setupconst Manipulator = const dotenvM =
// complete setupconst Manipulator = const dotenvM = '/path/to/project' false 'utf-8'
// undefined = default valueconst Manipulator = const dotenvM = undefined true 'latin1'
Manipulate
Add
Add object keys and values to .env file and process.env
const obj = REMOTE: '95.81.123.228' PORT: 3000 dotenvMconsole //=> '95.81.123.228'console //=> '3000'
.env file :
PORT=3000REMOTE=95.81.123.228
Update
To update a variable use
dotenvM
Example
Before
-
console //=> '95.81.123.228'
-
PORT=300REMOTE=95.81.123.228
Then
dotenvM
After
-
console //=> '38.10.10.25'
-
PORT=300REMOTE=38.10.10.25
Remove
Remove variable(s) from .env
file and process.env
dotenvM
Inputs are keys from an object, an array or a string :
here's three way to achieve the same result.
// #1dotenvMdotenvM // #2dotenvM // #3dotenvM
Try catch and DEBUG
Here's two example :
// DEBUGGINGdotenvMthrowable = false let debug = dotenvMlet debug_1 = dotenvMconsole//=> [ADD_ERROR]: object must be [object Object] but received [object Array]console//=> undefined
// TRY CATCHdotenvMthrowable = true try dotenvM catche console //=> [ADD_ERROR]: object must be [object Object] but received [object Array]
API
Full documentation available here