Giter Site home page Giter Site logo

grimen / node-env-file Goto Github PK

View Code? Open in Web Editor NEW
121.0 5.0 18.0 34 KB

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

Home Page: https://npmjs.org/package/node-env-file

License: MIT License

Makefile 1.88% JavaScript 98.12%

node-env-file's Introduction

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=
  # QUUX=

.env2

  # some env variables using exports syntax

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

index.js

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

  process.env.FOO = "defaultfoo";

  // Load any undefined ENV variables from 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, "");
  assert.equal(process.env.QUUX, undefined);

  // 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, "");
  assert.equal(process.env.QUUX, undefined);
  
  // Load any undefined ENV variables from a specified file, but don't crash if the file doesn't exist
  // Usefull for testing env vars in development, but using "real" env vars in production
  envfile(__dirname + '/.env', {raise: false});
  

API

  • (filepath)

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

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

Installation

  $ npm install node-env-file

Test

Local tests:

  $ make test

Examples

Local examples:

  $ make example

Related Libraries

License

Released under the MIT license.

Copyright (c) Jonas Grimfelt

Bitdeli Badge

node-env-file's People

Contributors

digitalsadhu avatar grimen avatar morriphi avatar tomayac avatar twistedstream avatar whappbook avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

node-env-file's Issues

# chars break parsing

I have an api key in my .env file that has a # character in it. It doesn't show up in process.env unless I remove the # from it.

eg.
SENDGRID_API_KEY=aWef#a12f

possible regression of V0.1.6 vs v0.1.5

Hi grimen,

I have just found a possible regression of node-env-file v0.1.6 vs v0.1.5;
Please find below what I found out.

Can you confirm the issue?

Thanks,
Maldestor95

code for

App.js

var env = require('node-env-file');
console.log('***');
try{
    env('process.env',{verbose: true, overwrite: true});
    console.log('***processed***');
    console.log ('NODE_ENV='+process.env.NODE_ENV);
    console.log(process.env.NODE_ENV);

}
catch (e) {
    console.log("ERROR "+e);
}

process.env

NODE_ENV=development
OTHER_ENV=FOO

Console output

"node-env-file": "0.1.5"

image

"node-env-file": "0.1.6"

image

Examples broken, Client use broken...

Hi there,

So far, I spent a few minutes trying to get this to work, even with the examples, but it seems to be broken... .env with no changes...

$ node basic.js 
[ENV]: Loading environment: /tmp/node-env-file/examples/.env
[ENV]: { FOO: 'foo1' }
[ENV]: { FOO: 'foo1', BAR: 'bar1' }
[ENV]: { FOO: 'foo1', BAR: 'bar1', BAZ: '1' }
[ENV]: { FOO: 'foo1', BAR: 'bar1', BAZ: '1', QUX: '' }

assert.js:92
  throw new assert.AssertionError({
        ^
AssertionError: "foo1" == "defaultfoo"
    at Object.<anonymous> (/tmp/node-env-file/examples/basic.js:8:8)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

$ cat .env
# some env variables

FOO=foo1
BAR=bar1
BAZ=1
QUX=

The other file .evn2 with an additional empty key... changed done on Ubuntu linux...

$ node basic.js 

assert.js:92
  throw new assert.AssertionError({
        ^
AssertionError: "exports MARC=" == ""
    at Object.<anonymous> (/tmp/node-env-file/examples/basic.js:18:8)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

$ cat .env2 
# some env variables using exports syntax

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

Thanks!

Duplicating data in the output

During the output of the log, duplicate the read variables.
Problem on string:123
module.exports.logger.info('[ENV]:', module.exports.data[env_file]);
not enough set of the key of variable
module.exports.logger.info('[ENV]:', module.exports.data[env_file][env_key]);

can't set false !

Given a file containing

RESPOND_TO_EDITED=false

process.env.RESPOND_TO_EDITED is set to 'false'

which is literally 'false', but unfortunately truthy ...

Feature request: option to allow non-existing .env file

I use a .env file for development, but not for production (where I set the environment variables through AWS mechanisms).

So my problem is that I need to wrap my code in production to detect a missing .env file. I'd rather just have noting happen if the .env file was not there.

I can do the changes and send a PR. Is it a feature that would be accepted?

Thanks,
Martin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.