Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: support dynamic variable lookup / inject whole process.env #197

Closed
ghost opened this issue May 21, 2020 · 2 comments
Closed

Comments

@ghost
Copy link

ghost commented May 21, 2020

I recently refactored my config logic to use the following code:

/** Get the named environment variable or return the provided default */
const getEnv = (name, backup) => process.env[name] || backup;

/** Ensure the environment variable is defined and return its value, or throw if it is undefined
 *
 * The environment variable is allowed to be empty (or null), as long as it is defined.
 */
const getAndEnsurePresentEnv = (name) => {
  if (process.env[name] === undefined) {
    throw new Error(`Environment variable "${name}" is not defined but is marked as required, aborting.`);
  }
  return process.env[name];
};

Previously, I was doing the same but manually:

// For each variable
export const API_URL = process.env.API_URL || 'http://localhost:8080/';

This refactoring broke the build, since dotenv-webpack only populates explicit variables like process.env.API_URL and not process.env[name] where name = "API_URL".

Would it be possible to add a configuration flag to allow loading all variables defined in the env file, and replacing the whole process.env? Something like

    new DotenvPlugin({
      // load variables from this file
      path: envPath,
      // inject ALL variables, not only the variables explicitly used
      loadAllVariables: true,
    })
@dmca-glasgow
Copy link

dmca-glasgow commented Jul 29, 2020

I was confused by this too when trying this package and using some existing env helper functions, you just need to change your api slightly:

export const env = strVar(process.env.NODE_ENV, 'NODE_ENV'); // pass process.env.NODE_ENV to your helper fn 

function strVar(envVar: string | undefined, key: string, allowEmptyValue = false): string {
  const value = envVar || '';
  if (!allowEmptyValue && value.length === 0) {
    throw new Error(`[env]: ${key} has invalid value: ${envVar}`);
  }
  return value;
}

@mrsteele
Copy link
Owner

Unfortunately this has been extensively tested. Here you can see my notes as to why this simply isn't possible and the problem is primarily due to webpack: #70 (comment).

@dmca-glasgow sounds like a workaround might be available, but unfortunately this will have to be left alone.

I'm going to close this as its a duplicate of #70 but let me know if you disagree and i'll be happy to open this back up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants