Skip to content

Dynamically loads environment variables from modules and then spawns a child process with them

Notifications You must be signed in to change notification settings

dawsontoth/dynenv

Repository files navigation

DynEnv

Dynamically loads environment variables from modules and then spawns a child process with them

It's like dotenv, but instead of .env files, you require modules!

Usage

Add dynenv as a dev dependency using your particular package manager:

npm install dynenv --save-dev
yarn add dynenv --dev

Say you had a file, snippet.js, with the following:

module.exports = function() {
  return {
    REACT_APP_SAY_WHAAAAAT: 'WORLD!',
  };
};

... and a react app. You know how to use environment variables in your app, right?

<title>Hello %REACT_APP_SAY_WHAAAAAT%!</title>

Now put everything together in your package.json by invoking dynenv!

  "scripts": {
    "start": "dynenv ./snippet.js -- react-scripts start",

Everything before the -- will be one-by-one required and given the opportunity to augment the environment. Then, whatever comes after will be spawned with the newly populated environment variables!

Usage in Third Party Modules

If you are crafting a module that can inject environment variables through dynenv, instead of forcing clients to reference a particular file in their call to dynenv, you can modify your package.json to point at that file. dynenv will find and invoke your script automatically.

{
  ...
  "dynenv": "build/cli/index.js",
  ...
}

About

Dynamically loads environment variables from modules and then spawns a child process with them

Resources

Security policy

Stars

Watchers

Forks

Packages

No packages published