12/04/2026 18:17pm

JS2GO EP.26 Using Environment Variables and Configurations in Go and Node.js
#Configuration
#Node.js
#Go
#Environment Variables
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.
1. Environment Variables in Node.js
Node.js can access environment variables through process.env.
Example: Reading an environment variable
// Assuming the terminal sets the environment variable
// export PORT=3000
const port = process.env.PORT || 8080;
console.log(`Server will run on port: ${port}`);
Using .env file with dotenv
// Install dotenv
// npm install dotenv
require('dotenv').config();
const port = process.env.PORT || 8080;
const dbUser = process.env.DB_USER;
const dbPassword = process.env.DB_PASSWORD;
console.log(`Connecting to DB as ${dbUser}`);
Advantages of Node.js Environment Variables
✔️ Easy access via process.env
✔️ Convenient usage with .env files and libraries like dotenv
✔️ Supports multiple environments: development, staging, production
Cautions
⚠️ Never commit .env files to the repository
⚠️ Set default values for critical environment variables
2. Environment Variables in Go
Go can access environment variables via the os package.
Example: Reading an environment variable
package main
import (
"fmt"
"os"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
fmt.Println("Server will run on port:", port)
}
Using configuration files and environment variables
import (
"fmt"
"github.com/joho/godotenv"
"os"
)
func main() {
err := godotenv.Load(".env")
if err != nil {
fmt.Println("Error loading .env file")
}
dbUser := os.Getenv("DB_USER")
dbPassword := os.Getenv("DB_PASSWORD")
fmt.Printf("Connecting to DB as %s\n", dbUser)
}
Advantages of Go Environment Variables
✔️ Type-safe when converting values to integer, boolean, etc.
✔️ Works well with system environment variables
✔️ Supports multiple environments via .env files or config structs
Cautions
⚠️ Convert environment variable values carefully to the correct type
⚠️ Never store credentials directly in the code
3. Best Practices
✔️ Store sensitive values like API keys, passwords, and DB credentials in environment variables
✔️ Set default values for critical variables to avoid errors
✔️ Use configuration management libraries:
- Node.js: dotenv, convict
- Go: godotenv, viper
✔️ Separate configuration by environment (development, staging, production)
✔️ Do not commit.envor other secret files to the repository
4. Comparison: Go vs Node.js
| Feature | Node.js | Go |
|---|---|---|
| Access | process.env | os.Getenv() |
| Config Files | dotenv | godotenv, viper |
| Type Safety | ❌ No | ✔️ Yes (manual conversion) |
| Multiple Environment | Supported | Supported |
| Recommended | Web app / rapid development | Backend / server / type-safe |
Recommendations
✔️ For frontend / backend web apps → Use Node.js + dotenv
✔️ For backend services / CLI / server → Use Go + godotenv or viper
Next Episode
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.
Read more
🔵 Facebook: Superdev Academy
🔴 YouTube: Superdev Academy
📸 Instagram: Superdev Academy
🎬 TikTok: https://www.tiktok.com/@superdevacademy?lang=th-TH
🌐 Website: https://www.superdevacademy.com/en