.env<\/em> file and update it with their environment variables.<\/p>\n\n\n\nEnvironment Manager Class<\/h2>\n\n\n\n As we already saw, environment variables can be used in the code by doing process.env. object. However when you have a lot of environment variables, this doesn\u2019t scale well. Besides some variables may have default values. It is a good practice to use EnvManager class.<\/p>\n\n\n\n
export default class EnvManager {\n public static getConfig = (name: string, defaultValue: any) => {\n return process.env[name] ?? defaultValue;\n };\n\n public static getNodeEnv = (defaultValue?: any) => {\n return this.getConfig(\"NODE_ENV\", defaultValue || \"development\");\n };\n\n public static getPort = (defaultValue?: any) => {\n return Number(this.getConfig(\"PORT\", defaultValue));\n };\n\n public static getDbHost = (defaultValue?: any) => {\n return this.getConfig(\"DB_HOST\", defaultValue);\n };\n\n public static getDbPort = (defaultValue?: any) => {\n return Number(this.getConfig(\"DB_PORT\", defaultValue));\n };\n\n public static getDbName = (defaultValue?: any) => {\n return this.getConfig(\"DB_NAME\", defaultValue);\n };\n\n public static getDbUsername = (defaultValue?: any) => {\n return this.getConfig(\"DB_USERNAME\", defaultValue);\n };\n\n public static getDbPassword = (defaultValue?: any) => {\n return this.getConfig(\"DB_PASSWORD\", defaultValue);\n };\n}<\/pre>\n\n\n\nEnvManager with JavaScript Proxy<\/h2>\n\n\n\n EnvManager class is a good way to manage environment variables in JavaScript, however, it may get a bit tedious to add a static method to the class whenever we are going to add a new variable. To automate this process we can use JavaScript Proxy. Let\u2019s take a look at how to do that.<\/p>\n\n\n\n
export default new Proxy(envManager, {\n get(envManager: EnvManager, field: string) {\n return function (defaultValue?: string | number | boolean) {\n if (field in envManager) {\n \/\/ if method exists on envManager (getDbConnectionUrl)\n return envManager[field](defaultValue);\n }\n let envVariable: string | number | undefined =\n process.env[studlyCaseToSnakeCase(field.replace(\"get\", \"\"))];\n\n if (envVariable && \/^true$\/i.test(envVariable)) {\n return true;\n }\n\n if (envVariable && \/^false$\/i.test(envVariable)) {\n return false;\n }\n\n if (envVariable && !isNaN(parseInt(envVariable, 10))) {\n (envVariable = parseInt(envVariable, 10));\n }\n return envVariable || defaultValue;\n };\n },\n});<\/pre>\n\n\n\nConclusion<\/h2>\n\n\n\n In conclusion, mastering the art of environment variables in JavaScript is not just about safeguarding sensitive data but also about streamlining your development process. By using dotenv files, you can keep your secrets safe and your code clean. The EnvManager class is a fantastic tool to efficiently access these variables, and if you want to take it a step further, the JavaScript Proxy method can automate and simplify the process.<\/p>\n\n\n\n
Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"
Environment variables in JavaScript are a set of key-value pairs that can be used to store data outside of the JavaScript code. This data can be anything from API keys to database credentials. Environment variables are often used to keep sensitive data secret, as they are not stored in the code itself. We can import…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[15],"tags":[24,17],"class_list":["post-30553","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-environment-variables","tag-javascript"],"yoast_head":"\n
Environment Variables in JavaScript | Alex Rusin Blog<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n