GithubHelp home page GithubHelp logo

dwyl / env2 Goto Github PK

View Code? Open in Web Editor NEW
100.0 29.0 9.0 88 KB

:computer: Simple environment variable (from config file) loader for your node.js app

License: GNU General Public License v2.0

JavaScript 99.45% Shell 0.55%

env2's People

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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

env2's Issues

Default file

Who has time to type env.json or .env? We should support a default file.

server can't find env2

Hi,

I am running a node express server, and have npm i --save env2 (it's in package.json and node_modules folder) but for some reason my sever keeps returning the error that it can't locate env2.
screen shot 2016-08-07 at 11 40 02

Do you know why this might be happening? Thanks!

Error: Cannot find module 'env2'

If you're getting the following error:

State changed from starting to crashed
Error: Cannot find module 'env2'
at Function.Module._resolveFilename (module.js:469:15)

image

env2 takes too much time load the variable

I am using env2 for loading env variable in nodejs+graphql setup and call the env loading function at the top of index.js file followed by schema for graphql server.

I am facing a problem where the env2 has not loaded the variable even after the method has been called like just after the line require('env2')(/path/to/env.json).

How does module.parent.id work?

Apparently "[t]ypically this is the fully resolved filename" of the requiring module. But why would there be a 'node_modules' in that path when we require from a module in our project? How is the id property different from the filename property?

Text Error

Should say instructions below, not above (with reference to the git ignore file)

Should we convert the string values coming from .env files to JS primitives?

At the moment things like: ENV_TEST_VAR=true read from .env files are string values in process.env.ENV_TEST_VAR.

Converting to primitive types would also make things more consistent with the case of using .json files.

An idea would be to update the add_dot_env_line_to_json function to:

function add_dot_env_line_to_json (json, env_variable) {  
  var environment_parts = env_variable.split('=');  
  try {  
    json[environment_parts[0]] = JSON.parse(environment_parts[1]);
  }
  catch(e){
    json[environment_parts[0]] = environment_parts[1];
  } 
  return json;
}

Let me know if this would suffice or if there's a better solution and I'll create a PR.

Confusing: not actually 12 factor app config approach

env2 looks like a nice way to manage config, it's just not the 12fa way to manage config. As it currently stands, env2 doesn't actually allow you to take in config from the environment! :)

Unless I'm missing something, 12fa explicitly calls out the pattern in env2 as an anti-pattern, and provides good reasons to avoid it:

Another approach to config is the use of config files which are not checked into revision control, such as config/database.yml in Rails. This is a huge improvement over using constants which are checked into the code repo, but still has weaknesses: it’s easy to mistakenly check in a config file to the repo; there is a tendency for config files to be scattered about in different places and different formats, making it hard to see and manage all the config in one place. Further, these formats tend to be language- or framework-specific.

Env vars are easy to change between deploys without changing any code; unlike config files, there is little chance of them being checked into the code repo accidentally; and unlike custom config files, or other config mechanisms such as Java System Properties, they are a language- and OS-agnostic standard.

Feature: Return env as Object

Story

As a person wanting to know which which environment variables were in the .env file
I would like to have the environment variables that were in the .env file returned as an Object
So that I can check if the var was loaded from a file or from another place.

Currently in our usage instructions: https://github.com/dwyl/env2#use-in-your-code
image
We hint at the possibility that the environment variables loaded by env2
are assigned to the env constant:

const env = require('env2')('./path-to-your/.env');

But reading the code we can see that no such Object is being returned:

env2/lib/env.js

Lines 57 to 60 in 15a8bd4

console.warn(msg);
return msg;
}
};

We could return the env on line 38 and it would (before the catch statement):

env2/lib/env.js

Lines 32 to 38 in 15a8bd4

var env = env_getter(filepath);
Object.keys(env).forEach(function(k) {
if(!process.env[k]) { // allow enviroment to take precedence over env.json
process.env[k] = env[k]; // only set if not set by environment
}
});
}

This would not alter the existing functionality of the package and would add this feature.

Why decache?

I don't think that we keep any state within env2 (apart from loading in path and fs; do they keep state?). Why is it necessary to decache the module?

Could use codecov key as good application example of env2

API keys may be a hard to understand example of using env2 because a lot of API applications would want more than local hosting. However in the case of using codecov, having your keys held locally would not cause problems for a live project and therefore could be a great example for env2.

Readme requires tidy up.

Readme needs some help...

  • badges specifically codecov
    env2-badges

  • What section appears to have been affected by a PR/Merge conflict:
    env2-what-section-fail

Anything else you think could be improved. (please comment in this issue before making changes. thanks!)

Best practice for .env files with travis continuous deployment

Currently I am using travis's repository environment variables, and these work perfect for the build process. However, upon trying to deploy (to app engine managed vm's in my case) those are not carried over. I see everywhere people suggesting to use a .env file or similar methods to store them. This makes sense for local, but if I want them to be available for deployment what is the best method?

I have one idea, but not sure how secure or ideal it is.
Basically since I don't want my .env on a public git repository, I have it added to my .gitignore. However, this means when a travis build triggers, there will be no .env cloned into the build enforcement. However, in theory I could put all my env's on the travis repository settings, and then as part of the travis script before deployment, copy those values and generate a .env file each build that will be deployed (of course making sure not to display them in the build output). This way no one would have access (I think) and I could manage all my variables from the Travis web interface.

Concerns:

  1. Not sure how to go about accessing them, but I found this regarding travis access token which seems like it would work. (https://github.com/travis-ci/travis.rb#build-environment-variables)
  2. Assuming I can access them, what is the best way to output them to a temp file securely.

Thank you

Environment variables with '=' won't work in '.env' files

If you are using a .env version of the file and have a '=' in the value of the environment variable, you will not get the correct value of the environment variable, because of the fact that only the second segment is used after splitting on '=' (

json[environment_parts[0]] = environment_parts[1];
).

I will fix this.

Build error using webpack with env2

I npm installed env2 into a project to configure and access my environment variables. When I go to run the build with webpack, it keeps throwing an error:

WARNING in ./~/env2/lib/env.js
Critical dependencies:
31:69-76 require function is used in a way in which dependencies cannot be statically extracted
 @ ./~/env2/lib/env.js 31:69-76

ERROR in ./~/env2/package.json
Module parse failed: /node_modules/env2/package.json Line 2: Unexpected token :
You may need an appropriate loader to handle this file type.
| {
|   "name": "env2",
|   "version": "2.0.6",
|   "description": "Environment Variable Loader",
 @ ./~/env2/lib/env.js 82:18-44

I included a plugin in the webpack.development.config.js file but the problem still persists. Plugin:

new webpack.DefinePlugin({
    'process.env.NODE_ENV': '"development"'
    })

I was wondering if anyone has had a similar issue?

Document what is meant by `'./path-to-your/.env'`

(ie path begins with ./ or ../ and is relative to the package.json or run directory)

It is non-intuitive within a complex file structure whether env vars are failing to load due to the wrong path being included or due to:

�[1m�[43m�[30m WARNING: �[22m�[42m�[30m env2 was required to load an .env file: �[46m�[30m ./.env �[1m�[43m�[30m INVALID JSON! �[22m�[42m�[30m Please see: http://git.io/vG3UZ�[49m�[39m�[22m

Documenting the above would help.

Streamline the Warning Message to avoid cluttering people's consoles!

At present we are printing out a large block of text:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Error: ENOENT, no such file or directory '/home/travis/build/nelsonic/hits/config.env'
Your app has invoked env2 to load a configuration file but 
we could not find the configuration file: /home/travis/build/nelsonic/hits/config.env
please follow the instructions in the README to create your
env.json file and/or ensure that you give the correct path to it 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

This is an eyesore. lets fix it.

See:

  • env2/lib/env.js

    Lines 41 to 48 in a55f917

    var msg = '\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n'
    msg += e
    msg += '\nYour app has invoked env2 to load a configuration file but \n'
    msg += 'we could not find the configuration file: ' + filepath + '\n'
    msg += 'please follow the instructions in the README to create your\n'
    msg += 'env.json file and/or ensure that you give the correct path to it \n'
    msg += '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n\n'
    console.log(msg);
  • https://travis-ci.org/nelsonic/hits/builds/77237644#L213-L219

Shameless plug

Hey @nelsonic, check out my defaultenv package I've created to solve the same problems -- I think you might like my approach:

  • It also allows you to use .js files to export default variables, so you can compute values dynamically if need be
  • It doesn't touch your app code -- instead it sets environment variables and then runs any command (e.g. defaultenv env/dev.js env/local.js -- npm start)

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.