Options
All
  • Public
  • Public/Protected
  • All
Menu

dotenv Manipulator :

  • Load environment variables from a .env to process.env
  • Add, update, or remove variables from both your .env file and process.env at runtime
example
// require
const Manipulator = require('dotenv-manipulator').default
example
// import
import Manipulator from 'dotenv-manipulator'

Hierarchy

  • Manipulator

Index

Constructors

Properties

Methods

Constructors

constructor

  • new Manipulator(envPath?: string, throwable?: boolean, encoding?: "ascii" | "base64" | "binary" | "hex" | "latin1" | "ucs-2" | "ucs2" | "utf-8" | "utf8" | "utf16le"): Manipulator
  • Starts dotenv Manipulator

    example
    // basic setup
    const Manipulator = require('dotenv-manipulator')
    const dotenvM = new Manipulator()
    example
    // complete setup
    const Manipulator = require('dotenv-manipulator')
    const dotenvM = new Manipulator('/path/to/project', false, 'utf-8')
    example
    // undefined = default value
    const Manipulator = require('dotenv-manipulator')
    const dotenvM = new Manipulator(undefined, true, 'latin1')

    Parameters

    • Default value envPath: string = path.normalize(path.resolve(__dirname, '.env'))

      Path to .env, default is __dirname.

    • Default value throwable: boolean = false

      If set to true functions throw errors whenever there's an incorrect input.

    • Default value encoding: "ascii" | "base64" | "binary" | "hex" | "latin1" | "ucs-2" | "ucs2" | "utf-8" | "utf8" | "utf16le" = "utf-8"

      Specify file encoding, default is utf8.

    Returns Manipulator

Properties

encoding

encoding: "ascii" | "base64" | "binary" | "hex" | "latin1" | "ucs-2" | "ucs2" | "utf-8" | "utf8" | "utf16le" = "utf-8"

env

env: {}

Type declaration

  • [x: string]: string

env_path

env_path: string

throwable

throwable: boolean

Methods

add

  • add(obj: {}, force?: boolean): undefined | TypeError
  • Add object keys and values to .env file and process.env

    example
    const obj = { REMOTE: '95.81.123.228', PORT: 3000 }
    dotenvM.add(obj)
    example
    // without force argument
    console.log(process.env.REMOTE_PORT) //=> '3000'
    dotenvM.add({ REMOTE_PORT: '2000' })
    console.log(process.env.REMOTE_PORT) //=> '3000'
    
    // with force argument set to true
    dotenvM.add({ REMOTE_PORT: '2000' }, true)
    console.log(process.env.REMOTE_PORT) //=> '2000'
    example
    // DEBUGGING #1
    dotenvM.throwable = false
    
    let debug = dotenvM.add(['wrong', 'type', 'of', 'data'])
    let debug_1 = dotenvM.add({ GOOOD: 'type', OF: 'data' })
    console.log(debug.message) //=> [ADD_ERROR]: object must be [object Object] but received [object Array]
    console.log(debug_1) //=> undefined
    
    // DEBUGGING #2
    dotenvM.throwable = true
    
    try {
       dotenvM.add(['wrong', 'type', 'of', 'data'])
    } catch(e) {
       console.log(e.message) //=> [ADD_ERROR]: object must be [object Object] but received [object Array]
    }

    Parameters

    • obj: {}

      Object to add to the environment

      • [x: string]: string
    • Default value force: boolean = false

      If you try to add a key/value pair that is already in the environment :
      - add(obj, true) would update said variables
      - add(obj) would just ignore your input

    Returns undefined | TypeError

remove

  • remove(obj: {} | string | string[]): undefined | TypeError
  • Remove variable from .env file and process.env

    example
    // #1
    dotenvM.remove('REMOTE')
    dotenvM.remove('PORT')
    
    // #2
    dotenvM.remove( { REMOTE: 'value that dotenvM wont read', PORT: 'no.. really it doesnt care' } )
    
    // #3
    dotenvM.remove(['REMOTE', 'PORT'])

    Parameters

    • obj: {} | string | string[]

      Key(s) you want to remove

    Returns undefined | TypeError

Legend

  • Constructor
  • Property
  • Method

Generated using TypeDoc