Skip to content

Instantly share code, notes, and snippets.

@colinfwren
Last active February 6, 2019 20:30
Show Gist options
  • Save colinfwren/6874e61fca79027a09ecde507c25c6f6 to your computer and use it in GitHub Desktop.
Save colinfwren/6874e61fca79027a09ecde507c25c6f6 to your computer and use it in GitHub Desktop.
Safely get Environment Variables
const getEnvVar = (key, safeDefault) => {
if (Object.prototype.hasOwnProperty.call(process.env, key) && !(typeof (process.env[key]) === 'undefined')) {
return process.env[key];
}
return safeDefault;
};
// Use it like
const config = {
serverUrl: getEnvVar('SERVER_URL', 'http://localhost:8080')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment