How to Read Environment Variables from Bnlang

In Bnlang, you can read environment variables using the built‑in process.env object.
It stores key–value pairs provided by the operating system or set manually before running the program.


Example: Reading a Variable

// file: env.bnl
const user = process.env.USER || "Guest";

console.log("Current user:", user);
USER=Alice bnl env.bnl
# Output: Current user: Alice

Example: Using Multiple Variables

// file: config.bnl
const host = process.env.HOST || "localhost";
const port = process.env.PORT || 3000;

console.log(`Server running at http://${host}:${port}`);
HOST=127.0.0.1 PORT=8080 bnl config.bnl
# Output: Server running at http://127.0.0.1:8080

Best Practices

  • Always provide default values for environment variables.
  • Use .env files with a loader for local development.
  • Keep sensitive values like API keys secret.
  • Document required variables for each project.