- Auto-load
.envon instantiation - Custom file loading
exportprefix support- Inline comments (respects quoted values)
- Single & double quoted values
- Variable interpolation (
${VAR}) - Set/unset variables in the system environment
- Required variable stubs
ringpm install dotenvload "dotenv.ring"
dotenv = new dotenv() // auto-loads .env
username = dotenv.getEnv("USERNAME")
print(username)| Method | Description |
|---|---|
loadDefault() |
Load the default .env file |
loadFile(path) |
Load a custom env file |
getEnv(key) |
Get a variable (returns NULL if missing) |
getEnvOr(key, default) |
Get a variable with a fallback |
hasKey(key) |
Check if a key was loaded |
getAll() |
Return all loaded key-value pairs |
setEnvVars() |
Push loaded vars into the system environment |
unsetEnvVars() |
Remove loaded vars from the system environment |
requiredVar(name) |
Append a stub to .env if the key is missing |
# Comments are ignored
export API_KEY=secret # export prefix is stripped
QUOTED="hello world" # quotes are removed
INLINE=value # with comment # inline comment stripped
BASE_URL=https://api.example.com
FULL_URL=${BASE_URL}/v1 # variable interpolationdotenv = new dotenv()
dotenv.loadFile("config/.env.production")port = dotenv.getEnvOr("PORT", "3000")if dotenv.hasKey("SECRET_KEY") {
print("Secret is configured\n")
}for item in dotenv.getAll() {
print("#{item[1]} = #{item[2]}\n")
}dotenv.setEnvVars()
print(sysget("USERNAME"))
dotenv.unsetEnvVars()dotenv.requiredVar("DATABASE_URL") // appends "DATABASE_URL=" to .env if missingSee the examples directory for runnable samples.
MIT. See LICENSE for details.