[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-ep-26-environment-variables-configurations-go-nodejs-all--*":3,"academy-blog-translations-uztt7ykgee8lm0c":75},{"data":4,"page":74,"perPage":74,"totalItems":74,"totalPages":74},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":68,"keywords":69,"locale":44,"published_at":70,"scheduled_at":13,"school_blog":66,"short_description":71,"slug":72,"status":64,"title":6,"updated":73,"updated_by":13,"views":67},"JS2GO EP.26 Using Environment Variables and Configurations in Go and Node.js","sclblg987654321","school_blog_translations","\u003Cp>Managing application configuration is crucial for security and flexibility, such as storing API keys, database credentials, or various settings. Using Environment Variables is the best practice for managing configuration without hardcoding sensitive information. In this article, we will show how to use environment variables and configuration management in Go and Node.js, with practical code examples and best practices.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>1. Environment Variables in Node.js\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Node.js can access environment variables through \u003Ccode inline=\"\">process.env\u003C\u002Fcode>.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Example: Reading an environment variable\u003C\u002Fp>\u003Cp>\u002F\u002F Assuming the terminal sets the environment variable\u003Cbr>\u002F\u002F export PORT=3000\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-javascript\">const port = process.env.PORT || 8080;\nconsole.log(`Server will run on port: ${port}`);\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Using \u003Ccode inline=\"\">.env\u003C\u002Fcode> file with dotenv\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-javascript\">\u002F\u002F Install dotenv\n\u002F\u002F npm install dotenv\n\nrequire('dotenv').config();\n\nconst port = process.env.PORT || 8080;\nconst dbUser = process.env.DB_USER;\nconst dbPassword = process.env.DB_PASSWORD;\n\nconsole.log(`Connecting to DB as ${dbUser}`);\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Advantages of Node.js Environment Variables\u003Cbr>✔️ Easy access via \u003Ccode inline=\"\">process.env\u003C\u002Fcode>\u003Cbr>✔️ Convenient usage with \u003Ccode inline=\"\">.env\u003C\u002Fcode> files and libraries like dotenv\u003Cbr>✔️ Supports multiple environments: development, staging, production\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Cautions\u003Cbr>⚠️ Never commit \u003Ccode inline=\"\">.env\u003C\u002Fcode> files to the repository\u003Cbr>⚠️ Set default values for critical environment variables\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>2. Environment Variables in Go\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Go can access environment variables via the \u003Ccode inline=\"\">os\u003C\u002Fcode> package.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Example: Reading an environment variable\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport (\n    \"fmt\"\n    \"os\"\n)\n\nfunc main() {\n    port := os.Getenv(\"PORT\")\n    if port == \"\" {\n        port = \"8080\"\n    }\n    fmt.Println(\"Server will run on port:\", port)\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Using configuration files and environment variables\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">import (\n    \"fmt\"\n    \"github.com\u002Fjoho\u002Fgodotenv\"\n    \"os\"\n)\n\nfunc main() {\n    err := godotenv.Load(\".env\")\n    if err != nil {\n        fmt.Println(\"Error loading .env file\")\n    }\n\n    dbUser := os.Getenv(\"DB_USER\")\n    dbPassword := os.Getenv(\"DB_PASSWORD\")\n    fmt.Printf(\"Connecting to DB as %s\\n\", dbUser)\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Advantages of Go Environment Variables\u003Cbr>✔️ Type-safe when converting values to integer, boolean, etc.\u003Cbr>✔️ Works well with system environment variables\u003Cbr>✔️ Supports multiple environments via \u003Ccode inline=\"\">.env\u003C\u002Fcode> files or config structs\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Cautions\u003Cbr>⚠️ Convert environment variable values carefully to the correct type\u003Cbr>⚠️ Never store credentials directly in the code\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>3. Best Practices\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>✔️ Store sensitive values like API keys, passwords, and DB credentials in environment variables\u003Cbr>✔️ Set default values for critical variables to avoid errors\u003Cbr>✔️ Use configuration management libraries:\u003C\u002Fp>\u003Cul>\u003Cli>Node.js: dotenv, convict\u003C\u002Fli>\u003Cli>Go: godotenv, viper\u003Cbr>✔️ Separate configuration by environment (development, staging, production)\u003Cbr>✔️ Do not commit \u003Ccode inline=\"\">.env\u003C\u002Fcode> or other secret files to the repository\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Chr>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>4. Comparison: Go vs Node.js\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cfigure class=\"table\">\u003Ctable>\u003Cthead>\u003Ctr>\u003Cth>Feature\u003C\u002Fth>\u003Cth>Node.js\u003C\u002Fth>\u003Cth>Go\u003C\u002Fth>\u003C\u002Ftr>\u003C\u002Fthead>\u003Ctbody>\u003Ctr>\u003Ctd>Access\u003C\u002Ftd>\u003Ctd>process.env\u003C\u002Ftd>\u003Ctd>os.Getenv()\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Config Files\u003C\u002Ftd>\u003Ctd>dotenv\u003C\u002Ftd>\u003Ctd>godotenv, viper\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Type Safety\u003C\u002Ftd>\u003Ctd>❌ No\u003C\u002Ftd>\u003Ctd>✔️ Yes (manual conversion)\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Multiple Environment\u003C\u002Ftd>\u003Ctd>Supported\u003C\u002Ftd>\u003Ctd>Supported\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Recommended\u003C\u002Ftd>\u003Ctd>Web app \u002F rapid development\u003C\u002Ftd>\u003Ctd>Backend \u002F server \u002F type-safe\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003C\u002Ffigure>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Recommendations\u003Cbr>✔️ For frontend \u002F backend web apps → Use Node.js + dotenv\u003Cbr>✔️ For backend services \u002F CLI \u002F server → Use Go + godotenv or viper\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>Next Episode\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>In EP.27 of JS2GO, we will learn about Logging: JavaScript vs Go to build an efficient event logging system and track issues in production professionally.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp data-start=\"498\" data-end=\"834\">\u003Cstrong>Read more\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FGolang\">\u003Cstrong>Golang The Series\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FJS2GO\">\u003Cstrong>JS2GO\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fen\u002Fblogs\u002Fcategories\u002FTailwind%20CSS\">\u003Cstrong>10 Eps That Will Make You a Pro Tailwind CSS Overnight\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>\u003Cstrong>🔵 Facebook: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🔴 YouTube: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.youtube.com\u002Fchannel\u002FUC2eI2RxcA2zbTqZyHyWIfRg\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>📸 Instagram: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.instagram.com\u002Fsuperdevschool\u002F\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🎬 TikTok: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\">\u003Cstrong>https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🌐 Website: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdevacademy.com\u002Fen\">\u003Cstrong>https:\u002F\u002Fwww.superdevacademy.com\u002Fen\u003C\u002Fstrong>\u003C\u002Fa>\u003Cstrong>&nbsp;\u003C\u002Fstrong>\u003C\u002Fp>","52_11zon_db0uxb14eq.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Ftoyykvszfn72z11\u002F52_11zon_db0uxb14eq.webp","2026-03-04 08:46:10.406Z","",{"keywords":15,"locale":38,"school_blog":48},[16,23,28,33],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:46:09.677Z","ha1ih9kkw3lzrvy","Configuration","2026-04-10 16:13:09.406Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:24:40.405Z","05u3tysava2z6ga","Node.js","2026-04-10 16:07:29.504Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:46:09.895Z","1zitau06ohn79bf","Environment Variables","2026-04-10 16:13:09.532Z",{"code":39,"collectionId":40,"collectionName":41,"created":42,"flag":43,"id":44,"is_default":45,"label":46,"updated":47},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"category":49,"collectionId":50,"collectionName":51,"expand":52,"id":66,"views":67},"hsa1afr8fcnd6qb","pbc_2105096300","school_blogs",{"category":53},{"blogIds":54,"collectionId":55,"collectionName":56,"created":57,"created_by":13,"id":49,"image":58,"image_alt":13,"image_path":59,"label":60,"name":61,"priority":62,"publish_at":63,"scheduled_at":13,"status":64,"updated":65,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:24:37.986Z","js2_go_2_11zon_y6paxmuz32.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fhsa1afr8fcnd6qb\u002Fjs2_go_2_11zon_y6paxmuz32.webp",{"en":61,"th":61},"JS2GO",10,"2025-08-11 03:41:08.820Z","published","2026-04-25 02:32:14.339Z","uztt7ykgee8lm0c",205,"toyykvszfn72z11",[20,25,30,35],"2025-10-09 01:25:40.829Z","Learn how to manage Environment Variables and application configuration in Go and Node.js, securely handling sensitive values such as API keys and database credentials, with practical code examples and best practices","ep-26-environment-variables-configurations-go-nodejs","2026-04-25 02:48:00.945Z",1,{"en":72}]