Giter Site home page Giter Site logo

kerimdzhanov / dotenv-flow Goto Github PK

View Code? Open in Web Editor NEW
847.0 12.0 36.0 446 KB

Loads environment variables from .env.[development|test|production][.local] files for Node.js® projects.

License: MIT License

JavaScript 96.56% TypeScript 3.44%
nodejs environment-variables dotenv node-env env configuration environment dotenv-flow dx express-js

dotenv-flow's People

Contributors

aymericbouzy avatar dependabot[bot] avatar dtinth avatar ericrallen avatar falkenhawk avatar jlandrum avatar jolg42 avatar kerimdzhanov avatar perrin4869 avatar snyk-bot avatar solaris765 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dotenv-flow's Issues

Great work ✨

This is a wonderful package doing just what I was looking for. Thank you for putting this together!

🍻

default_node_env: 'production' ignored

I start my script with:

require('dotenv-flow').config({
    default_node_env: 'production'
});

The default_node_env is ignored. NODE_ENV is not set.
Ubuntu 1804
node 13.7.0

Can't use without a default file

Currently we can't solely rely on .env.{NODE_ENV} files, we also need a default .env file. On my current project, there are no values that are default values. That would be understandable if my .env.{NODE_ENV} files were overriding the default values, but that isn't happening. So I end up needing to commit a blank .env file.

Would be nice to add functionality to -

  1. not require a default .env file (projects without this will throw an error - Error: ENOENT: no such file or directory, open '/Users/ssanders/dev/project/skii/middleware/.env')
  2. make sure that the files with NODE_ENV vars override the defaults

How to disable logs about already defined env

When I run my production app with docker docker run -e foo=1 -e bar=2 myapp always get the logs about already defined env like this

dotenv-flow: "foo" is already defined in `process.env` and will not be overwritten
dotenv-flow: "bar" is already defined in `process.env` and will not be overwritten

How can I disable these log.

Assigning as process.env.NODE_ENV = deployTarget has no effect

I tried to use in an Ember app for deployment and it seems like it fails to read the right values.
Here is how it is defined in my deploy.js:

'use strict';
require('dotenv').config();
require('dotenv-flow').config();

module.exports = function(deployTarget) {
  process.env.NODE_ENV = deployTarget || 'development;

 if (deployTarget === 'staging') {
    console.log('NODE-ENV: ' + process.env.NODE_ENV);
    ENV.s3 = {
      accessKeyId: process.env.AWS_ACCESS_KEY,
      secretAccessKey: process.env.AWS_SECRET_KEY,
      bucket: 'XXXX',
      region: 'eu-west-1'
    };

     ENV['s3-index'] = {
      accessKeyId: process.env.AWS_ACCESS_KEY,
      secretAccessKey: process.env.AWS_SECRET_KEY,
      bucket: 'XXXX',
      region: 'eu-west-1'
    };
  }
}

It fails to deploy with errors:

+- didFail
CredentialsError: Missing credentials in config
CredentialsError: Missing credentials in config
    at ClientRequest.<anonymous> (/Users/XXX/projects/XXX/XXXX/node_modules/aws-sdk/lib/http/node.js:83:34)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:106:13)
    at ClientRequest.emit (events.js:208:7)
    at Socket.emitTimeout (_http_client.js:706:34)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:106:13)
    at Socket.emit (events.js:208:7)
    at Socket._onTimeout (net.js:410:8)
    at ontimeout (timers.js:498:11)|
Pipeline aborted

I defined 2 separate files in the project root:

  • .env
  • `.env.staging``

What's wrong with that? Can't I assign a value to process.env.NODE_ENV = ...?
Thank you.

no error property set when .env file is not present

Upon calling .config(), dotenv sets an error property in the returned object when .env is not present.

var result = require("dotenv").config();

// result.error property is set when .env is not present in the root directory of the node project

dotenv-flow does not set an error property, and instead it only has an empty "parsed" object.

var result = require("dotenv-flow).config();

// restult.error is not present, but result.parsed is an empty object

It would be nice to have a consistent behavior as "dotenv"

Ability to change `NODE_ENV` option to switch .env.<NODE_ENV> files

I'm using NextJs with dotenv-flow and using non standard environments as staging and edge.

When trying to load my .env.staging or .env.edge set of env variables with NODE_ENV=staging|edge next dev it does "normalize" NODE_ENV variable to be development instead, and giving following warning:
image
see additional explanation here

It would be nice to have ability to provide alternative option so dotenv-flow can be used with it e.g. APP_ENV=staging next dev

If this sounds as a good idea I'll be happy to work on it.

default_node_env not setting NODE_ENV

When using 'default_node_env' v0.4 dotenv-flow in Windows with node v11.4.0, the default config option as it is not setting a NODE_ENV.

Downgraded to v0.1 and it works again.

cli executable

Hi Dan,
I'd like to introduce a cli executable which would work similar to https://www.npmjs.com/package/dotenv-cli (calling a command from the argv after setting ENV), but load dotenv-flow internally and possibly those -e and -p options.
My use case: calling k6 from cli with dotenv-flow k6 -- run load-tests.js

It would be a simple script, but will probably come with additional dependencies (cross-spawn and minimist, ...and maybe, just maybe dotenv-expand 🤔 although I personally don't need it atm)

Would you prefer to have it as a separate package dotenv-flow-cli or rather integrated into this repo? Maybe you have an idea how to do it better - with less or no dependencies? (to keep the zero-dependency policy)

Consider adding support for dotenv-expand

dotenv-expand allows developers to reference process.env variables inside their .env files.

For example:

IP=127.0.0.1
PORT=1234
APP_URL=http://${IP}:${PORT}

Using the above example .env file, process.env.APP_URL would be http://127.0.0.1:1234.

Allow -r dotenv-flow/config

I prefer do leave .env loading totally up to the user of the app so I don't have anywhere in my code require('dotenv').config(); but rather I use node -r dotenv/config app.js And if it runs in docker then docker takes care of injecting it and I dont have to change anything in my app code.

Unfortunatelly this doesn't work with this lib :(

Is this the 12 factor way?

The way I read this, it seems merging multiple env vars is contrary to the Twelve-Factor App.

https://12factor.net/config

In a twelve-factor app, env vars are granular controls, each fully orthogonal to other env vars. They are never grouped together as “environments”, but instead are independently managed for each deploy.

Can someone please enlighten me?

no default export.ts

This is my package.json

{
    "dependencies": {
        "dotenv-flow": "^3.2.0"
    },
    "devDependencies": {
        "@types/dotenv-flow": "^3.1.0"
    }
}

This is the code:

import dotenvFlow from 'dotenv-flow';
dotenvFlow.config();

This is the full error message:

Module '"/Users/<PROJECT>/cdk/node_modules/@types/dotenv-flow/index"' has no default export.ts(1192)

how can I fix it?

Is DOTENV_FLOW_SILENT supposed to suppress "already defined" messages?

I've tried a few different ways to silence these types of messages:

dotenv-flow: "DATABASE_HOST" is already defined in `process.env` and will not be overwritten
dotenv-flow: "DATABASE_PORT" is already defined in `process.env` and will not be overwritten
dotenv-flow: "DATABASE_NAME" is already defined in `process.env` and will not be overwritten
dotenv-flow: "DATABASE_USERNAME" is already defined in `process.env` and will not be overwritten
dotenv-flow: "DATABASE_PASSWORD" is already defined in `process.env` and will not be overwritten

Because I use dotenv-flow in several files, these messages get repeated many times.

I've tried setting DOTENV_FLOW_SILENT in my environment via PowerShell, and confirmed that it is indeed there. The documentation isn't clear on what the value is supposed to be, so I've set it to "silent" based on skimming the code.

PS C:\dev\myproject> Get-ChildItem Env:DOTENV_FLOW_SILENT

Name                           Value
----                           -----
DOTENV_FLOW_SILENT             silent

Adding the environment variable to my .env file also does not seem to do anything.

I am using version 3.2.0.

Am I doing something wrong with this setting?

.env is not working as aspected

hi guy's i have 3 different env file

  1. .env (this set up development and production env)
  2. .env.development (this contain all development configuration)
  3. .env,production (this contains all production configuration)

When i start node js it was unable to get NODE_ENV from .env file but working fine with dotenv with dotenv-flow , and i use dotenv with dotenv-flow .
Then dotenv-flow returns this warning
dotenv-flow: "NODE_ENV" is already defined in process.env and will not be overwritten
but when i only use dotenv-flow it was unable to get NODE_ENV from .env

 // This module is responsible for adding env variable from .env file

// Importing 3rd Party Module

import dotEnv from "dotenv";

import dotEnvFlow from "dotenv-flow";

// Dot Env will get all variable's from .env file
dotEnv.config();
dotEnvFlow.config();

// Export Dot Env Module
export { dotEnv , dotEnvFlow };

bug(load): Non-existent files fail silently

It looks like the load function fails silently on all files when one file doesn't exist because there is no error handling in the parse function. It seems like the preferred functionality would be to either "yell" about the error from the catch block in the load function or to add error handling inside of the iteration on lines 45-47 of lib/dotenv-flow.js so that each file is read independently, any errors are caught and console.warned, and then the parsed file list is filtered down to just the files that can be read.

I would be more than happy to make a PR, but I wasn't sure if there is a preferred behavior.

function parse(filenames, options = {}) {
if (typeof filenames === 'string') {
return dotenv.parse(fs.readFileSync(filenames, options));
}
return filenames.reduce((parsed, filename) => {
return Object.assign(parsed, dotenv.parse(fs.readFileSync(filename, options)));
}, {});
}

[pre-pull request] --mode switch

I didn't know where to put this and I didn't want to submit a pull request for unfinished / possibly unwanted additions.

BUT

I forked and added a --mode switch that gets parsed during the 'getDotenvFilenames' Fn that should make your module treat .env files the same as vue-cli. (this was more convenient for my team)

Basically if the switch exists it looks for .env.{mode} or .env.{mode}.local files instead of NODE_ENV files, though it will still default to using NODE_ENV as the mode.

function getDotenvFilenames(cwd) {
  const { NODE_ENV = 'development' } = process.env;

  let modeI = process.argv.indexOf("--mode")
  let mode = (modeI != -1 && process.argv.length > modeI + 1) ?
    process.argv[modeI + 1] :
    NODE_ENV

  return [
    NODE_ENV && `${cwd}/.env.${mode}.local`,
    NODE_ENV && `${cwd}/.env.${mode}`,
    NODE_ENV !== 'test' && `${cwd}/.env.local`,
    `${cwd}/.env`
  ].filter(filename => filename && fs.existsSync(filename));
}

If this seems like an addition that would fit I'll write up some actual testing cases before submitting a pull request.

(if you look in my fork you will also notice that I changed NODE_ENV to default to 'development', I don't plan on including this in a possible pull request.)

Process values on initialization

Hi there,

In my case I need to decode values of env variables through symmetric encryption, cause store passwords in open kind is bad in other side we can't use vault or other secret storage yet (inner infrastructure doesn't ready), so we pass the password from outsite.

Example:

// .env.production
DB_PASSWORD=ENV(96b69e27a5b662b0b98222a424485a35 ) // encrypted
// main.js
const dotenvFlow = require('dotenv-flow');
const decode = requre('decode');

const password = process.env.DOCKER_ENV_PASSWORD // Passed from outside

dotenvFlow.config({
  process: function(key, value) {
    return {
      key: decode(value.match(/\((.*)\)/).pop(), password);
    }
  }
});

What about this feature? Does it possible to do PR?

Using wrong .env file for specified NODE_ENV

I'm running into an issue using dotenv-flow version 0.1.0. I have a npm build script that sets the NODE_ENV environment variable using cross-env. For example,

cross-env NODE_ENV=development npm run build

In my root directory I have the following .env files...

  • .env.development
  • .env.test
  • .env.production

When I run my script I can see the NODE_ENV as being set to the correct environment (i.e., development, test, production). At the start of the build require('dotenv-flow').config() is called.

However, when I log one of the environment variables that is defined in all three of the files I am getting back the value from the .env.production file even though NODE_ENV is specified as development.

Seems like dotenv-flow is not respecting the NODE_ENV variable value. Any ideas?

.env subfolders

In my project's root, I have a .env folder where I store my .env.<environment> and .env.<environment>.local files. This works great when I pass the { cwd: '.env/' } option to config, but I'd like to have subfolders under .env that would hold each pair of environment files.

For example:

.env/
-----.env
-----.env.local
-----development/
----------.env.development
----------.env.development.local
-----staging/
----------.env.staging
----------.env.staging.local
-----production/
----------.env.production
----------.env.production.local

Changing the cwd option to .env/* or .env/**/* doesn't seem to work and my project fails to recognize variables not in .env or .env.local. Any ideas?

version bump to major

Because of semver and npm versioning pattern, the current version in a projects package.json of dotenv-flow: ^0.4.0 will install any package up to and including 1.x.x - so, if the library maintainers of this repo decide to bump a major version to 1.0.0 with breaking changes and all the projects out there aren't aware and they are using the carrot in their dependencies list for this package, their project will install the breaking change.

I recommend bumping to 1.0.0 and publishing to set the baseline for this package.

Why items in .env.local is not picked ?

A simple test case

# .env
WS_PORT=8090

# .env.development
WS_PORT=8092

# .env.local
WS_PORT=8091

# ./test/test.js
require('dotenv-flow').config()
console.log('ws port:', process.env.WS_PORT)

# package.json

"test": "NODE_ENV=development node test/test.js",
"test2": "node test/test.js",

npm run test or npm run test2 both ouput 8090, with message dotenv-flow: "WS_PORT" is already defined in process.env and will not be overwritten, but why ?

cannot use base64 encoded .env file to decode

I have tried mentioning option {encoding: 'base64'}. But its actually reading and then encoding to base64.
As per me possible behavior should be if a base64 encoded .env file is passed then package should decode it from base64 and then load in process.env in nodejs program.

If the current behavior is not and bug and is expected one then is there any way that I can read base64 encoded .env file and get decoded environment variables in process.env?

Specifying NODE_ENV in .env.local file

It's currently possible to set NODE_ENV by passing the --node-env option or by using cross-env.

However, it's not possible to set NODE_ENV in a .env file.

Example:

.env.local

NODE_ENV=development

.env.development.local

[development variables...]

This would be really helpful in cases where we can't hardcode the node environment or just want to run npm start and load the specific environment file depending on another .env file.

scripts: {
    "start": "node index.js"    // <-- instead of having start-dev, start-prod etc. one could just run the start script and specify the node environment in a .env file
}

Of course this would only work if NODE_ENV is read from files with lower priorities, i.e. .env and .env.local.

Error with symlink .env.local

Hello
it seems that file which are symlinks are not found and not loaded :(

$ ls -la
.env OK
.env.local -> /var/xxx/xxx/.env.local   NOK

Load different .env file based on other environment variable

Current

currently dotenv-flow only can load different .env file based on environment variable NODE_ENV

Expect

Load different .env file based on other environment variable, DEPLOY_ENV for example. So can we add a new option to support it?

Filename extension

I think this way of organizing environment-dependent variables has a ton of potential! One small nitpick, however, is that I've now lost support for .env file syntax highlighting on my IDE. If the file name structure was "flipped" and looked more like local.production.env, this wouldn't be an issue. There are probably complexities that I'm overlooking, but would this be possible?

npx mocha -r dotenv-flow/config --recursive app not working on 4.0.0 but was fine on 3.2.0

Hello, for some reason preload stopped working when i upgraded dotenv-flow from 3.2 to 4.0

i am running it on node 18, as long as i keep 3.2 things works fine.

Here's a sample error when running with 4.0:

[sombriks@lucien market-forecast]$ npx mocha -r dotenv-flow/config --recursive app

✖ ERROR: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './config' is not defined by "exports" in /home/sombriks/git/market-forecast/node_modules/dotenv-flow/package.json imported from /home/sombriks/git/market-forecast/node_modules/mocha/lib/nodejs/esm-utils.js
    at new NodeError (node:internal/errors:405:5)
    at exportsNotFound (node:internal/modules/esm/resolve:362:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:642:13)
    at packageResolve (node:internal/modules/esm/resolve:873:14)
    at moduleResolve (node:internal/modules/esm/resolve:939:20)
    at defaultResolve (node:internal/modules/esm/resolve:1132:11)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:835:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ESMLoader.import (node:internal/modules/esm/loader:524:22)
    at importModuleDynamically (node:internal/modules/cjs/loader:1188:29)
    at importModuleDynamicallyWrapper (node:internal/vm/module:429:21)
    at importModuleDynamically (node:internal/vm:106:46)
    at importModuleDynamicallyCallback (node:internal/process/esm_loader:36:14)
    at exports.doImport (/home/sombriks/git/market-forecast/node_modules/mocha/lib/nodejs/esm-utils.js:35:34)
    at formattedImport (/home/sombriks/git/market-forecast/node_modules/mocha/lib/nodejs/esm-utils.js:32:18)
    at exports.requireOrImport (/home/sombriks/git/market-forecast/node_modules/mocha/lib/nodejs/esm-utils.js:42:34)
    at exports.handleRequires (/home/sombriks/git/market-forecast/node_modules/mocha/lib/cli/run-helpers.js:94:34)
    at async /home/sombriks/git/market-forecast/node_modules/mocha/lib/cli/run.js:349:25 {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

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.