Last active
February 6, 2019 20:30
-
-
Save colinfwren/6874e61fca79027a09ecde507c25c6f6 to your computer and use it in GitHub Desktop.
Safely get Environment Variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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