Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Parse and load environment files (containing ENV variable exports) into Node.js environment, i.e. `process.env`.

branch: master

Merge pull request #7 from tomayac/patch-1

Trivial typo fix in an error message.
latest commit 6f1ec0e4ad
Jonas Grimfelt authored
examples Init.
lib Trivial typo fix in an error message.
test BUGFIX: `#` chars break parsing - fixes #6
.editorconfig Init.
.gitattributes Init.
.gitignore Init.
.travis.yml Init.
MIT-LICENSE Init.
Makefile Makefile: `make test-watch` task
README.md Add a Bitdeli Badge to README
TODO Init.
index.js Init.
package.json VERSION: 0.1.2
README.md

NODE-ENV-FILE Build Status

Parse and load environment files (containing ENV variable exports) into Node.js environment, i.e. process.env.

Example

.env

  # some env variables

  FOO=foo1
  BAR=bar1
  BAZ=1
  QUX=

.env2

  # some env variables using exports syntax

  exports FOO=foo2
  exports BAR=bar2
  exports BAZ=2
  exports QUX=

index.js

  var assert = require('assert');
  var env = require('node-env-file');

  process.env.FOO = "defaultfoo";

  // Load any undefined ENV variables form a specified file.
  env(__dirname + '/.env');
  assert.equal(process.env.FOO, "defaultfoo");
  assert.equal(process.env.BAR, "bar1");
  assert.equal(process.env.BAZ, "1");
  assert.equal(process.env.QUX, "");

  // Load another ENV file - and overwrite any defined ENV variables.
  env(__dirname + '/.env2', {overwrite: true});
  assert.equal(process.env.FOO, "foo2");
  assert.equal(process.env.BAR, "bar2");
  assert.equal(process.env.BAZ, "2");
  assert.equal(process.env.QUX, "");

API

  • (filepath)

    env('./path/to/.env');
    
  • (filepath, options)

    env('./path/to/.env', {verbose: true, overwrite: true});
    

Installation

  $ npm install node-env-file

Test

Local tests:

  $ make test

Examples

Local examples:

  $ make example

Related Libraries

Notes

Bash based approach:

Makefile

  define LOAD_ENV
    ENV_FILE=$${2:-'.env'}
    echo "LOAD: $$ENV_FILE"

    if [ -f $$ENV_FILE ]; then
      while read line || [ -n "$$line" ]; do
        if [[ "$$line" == *=* ]] && [[ "$$line" != #* ]]; then
          # TODO: handle exports case
          echo "  export $$line"
          eval "export $$line"
        fi
      done < "$$ENV_FILE"
    fi
  endef
  export LOAD_ENV

  env:
    eval "$$LOAD_ENV"

  ...

License

Released under the MIT license.

Copyright (c) Jonas Grimfelt

Bitdeli Badge

Something went wrong with that request. Please try again.