2.5.1 • Published 1 year ago

env-agent v2.5.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

env-agent

A zero-dependency package for reading environment variables into process.env at runtime. Easily parse, load and expand environment variables from .env files. Provides sanity checks when parsing .env files and ensures all incoming variables are defined before being added to process.env.

Unit Tests npm

Installation

npm:

$ npm install env-agent --save

yarn:

$ yarn add env-agent

Usage

Ensure you have a .env file in the root of your project. This file should contain all the environment variables you want to use in your project. For example:

# .env
PORT=3000
NODE_ENV=development

Then, in your project, import the env-agent package and call the load function:

It is highly recommended to call the load function as early as possible in your project. This will ensure all environment variables are available to your project as soon as possible.

CommonJS:

const envAgent = require('env-agent').default;

envAgent.load();

ES6:

import envAgent from 'env-agent';

envAgent.load();

// {
//   PORT: 3000,
//   NODE_ENV: 'development'
// }

Now all the environment variables defined in your .env file are available in process.env.

API

load([, options])

loads the environment variables defined in your .env file. This function should be called as early as possible in your project.

options

OptionTypeDefaultDescription
silentbooleantrueWhen attempting to load the .env file, specify whether errors should be thrown or not.
strictbooleanfalseEnsures variables are defined before being added to process.env.
pathstringprocess.cwd()The path to the .env file.
expandstring'none'Choose to expand variables defined in your .env file.
overwritebooleanfalseOverwrite existing environment variables.
encodingstring'utf8'The encoding of the .env file when reading.
debugbooleanfalseShow debug messages when loading the .env file.
templatestringundefinedDefine a template to validate the .env file against.
envAgent.load({
    silent: false,
    debug: true
});

parse(file)

Parses the input and returns an object containing the environment variables.

const env = envAgent.parse(`
    PORT=3000
    NODE_ENV=development
`);

// or

const env = envAgent.parse(Buffer.from('PORT=3000\nNODE_ENV=development'));

expand(variables, expansionMode)

Choose to expand variables defined in your .env file. It is recommened to expand variables when calling envAgent.load(). Select the expansion mode that best suits your needs.

  • none - No expansion will be performed.
  • project - Expand variables defined in the .env file (does not expand current process.env variables).
  • machine - Expand variables defined on your machine's environment (expands current process.env variables).
envAgent.load({ expand: "project" }) // variables will automatically expand

// or

const variables = envAgent.load();

envAgent.expand(variables, "project"); // variables will expand

You can denote a variable to be expanded by using the following syntax:

  • $VARIABLE
  • ${VARIABLE}
# .env
PORT=3000
NODE_ENV=development
HOST=localhost
URL=http://${HOST}:$PORT # HOST and PORT will be expanded
envAgent.load({ expand: "project" });

// {
//   PORT: 3000,
//   NODE_ENV: 'development',
//   HOST: 'localhost',
//   URL: 'http://localhost:3000'
// }

get(key)

Retrieve a single environment variable from process.env.

set(key, value[, overwrite])

Sets a single environment variable in process.env. Ensures configuration rules are followed:

  • If strict is true, the variable must be defined before being added to process.env.
  • If overwrite is false, the variable must not already exist in process.env.
  • If expand is true, the variable will be overwritten with the value.

delete(key)

Deletes a single environment variable from process.env.

CHANGELOG

See CHANGELOG.md

LICENSE

See LICENSE

2.5.1

1 year ago

2.5.0

1 year ago

2.4.0

1 year ago

2.3.1

1 year ago

2.3.0

1 year ago

2.2.0

1 year ago

2.1.2

1 year ago

2.1.1

1 year ago

2.1.0

1 year ago

2.0.0

1 year ago

1.2.0

2 years ago

1.1.1

2 years ago

1.0.0

2 years ago