jsrosetta

I/O

Environment Variables

How Node.js's process.env compares to os.Getenv (Go), env::var (Rust), ProcessInfo.environment (Swift), and System.getenv (Java).

Minimum versions
Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.58Swift ≥ 3.0Java ≥ 25
Verified on
Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1

Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.

Reading an environment variable is one of the simplest I/O operations there is, but access differs slightly: Node.js treats process.env like an object; Go, Rust, and Java all use a function that takes the variable's name, while Swift indexes the environment dictionary with a subscript. Go returns an empty string when the variable doesn't exist, Rust returns a Result (use unwrap_or_default() to get a similarly empty string), and Swift and Java return nil/null.

Reading an environment variable

const key = process.env['API_KEY']
 
console.log(key)
$ API_KEY=foobar node env_vars.js
foobar
 
$ API_KEY=foobar go run env_vars.go
foobar
 
$ API_KEY=foobar cargo run
foobar
 
$ API_KEY=foobar swift env_vars.swift
foobar
 
$ API_KEY=foobar java Main.java
foobar

Reference: github.com/miguelmota/golang-for-nodejs-developers#env-vars