Modules@robinpath/dotenv
dotenv

@robinpath/dotenv

0.1.1Node.jsPublic

Secure .env file management with key validation, path restrictions, and protected system variables

Dotenv

Secure .env file management with key validation, path restrictions, and protected system variables

Package: @robinpath/dotenv | Category: Utility | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the dotenv module when you need to:

  • Parse a .env format string into an object -- Use dotenv.parse to perform this operation
  • Convert an object to .env format string -- Use dotenv.stringify to perform this operation
  • Read a .env file and load values into process.env (won't override existing or protected vars by default) -- Use dotenv.load to perform this operation
  • Read a .env file and return as object without modifying process.env -- Use dotenv.read to perform this operation
  • Get a value from a .env file by key -- Use dotenv.get to perform this operation

Quick Reference

FunctionDescriptionReturns
parseParse a .env format string into an objectKey-value object
stringifyConvert an object to .env format string.env format string
loadRead a .env file and load values into process.env (won't override existing or protected vars by default)Actually loaded key-value pairs
readRead a .env file and return as object without modifying process.envKey-value object
getGet a value from a .env file by keyValue or null
setSet a key=value in a .env fileTrue on success
removeRemove a key from a .env fileTrue on success
existsCheck if a .env file existsTrue if exists
keysReturn all keys from a .env fileArray of key strings
expandExpand variable references like ${VAR} in values (process.env fallback disabled by default)Object with expanded values

Functions

parse

Parse a .env format string into an object

Module: dotenv | Returns: object -- Key-value object

dotenv.parse "KEY=value"
ParameterTypeRequiredDescription
contentstringYes.env format string

stringify

Convert an object to .env format string

Module: dotenv | Returns: string -- .env format string

dotenv.stringify $vars
ParameterTypeRequiredDescription
objobjectYesKey-value object

load

Read a .env file and load values into process.env (won't override existing or protected vars by default)

Module: dotenv | Returns: object -- Actually loaded key-value pairs

dotenv.load ".env"
ParameterTypeRequiredDescription
filePathstringNoPath to .env file (must be a .env file)
overridebooleanNoSet true to override existing vars (default: false)

read

Read a .env file and return as object without modifying process.env

Module: dotenv | Returns: object -- Key-value object

dotenv.read ".env.local"
ParameterTypeRequiredDescription
filePathstringNoPath to .env file

get

Get a value from a .env file by key

Module: dotenv | Returns: string -- Value or null

dotenv.get ".env" "DATABASE_URL"
ParameterTypeRequiredDescription
filePathstringYesPath to .env file
keystringYesKey to look up

set

Set a key=value in a .env file

Module: dotenv | Returns: boolean -- True on success

dotenv.set ".env" "PORT" "3000"
ParameterTypeRequiredDescription
filePathstringYesPath to .env file
keystringYesKey to set (must match [A-Za-z_][A-Za-z0-9_]*)
valuestringYesValue to set

remove

Remove a key from a .env file

Module: dotenv | Returns: boolean -- True on success

dotenv.remove ".env" "OLD_KEY"
ParameterTypeRequiredDescription
filePathstringYesPath to .env file
keystringYesKey to remove

exists

Check if a .env file exists

Module: dotenv | Returns: boolean -- True if exists

dotenv.exists ".env.local"
ParameterTypeRequiredDescription
filePathstringNoPath to check (must be a .env file)

keys

Return all keys from a .env file

Module: dotenv | Returns: array -- Array of key strings

dotenv.keys ".env"
ParameterTypeRequiredDescription
filePathstringNoPath to .env file

expand

Expand variable references like ${VAR} in values (process.env fallback disabled by default)

Module: dotenv | Returns: object -- Object with expanded values

dotenv.expand $vars
ParameterTypeRequiredDescription
varsobjectYesKey-value object with variable references
useProcessEnvbooleanNoAllow fallback to process.env for unresolved vars (default: false)

Error Handling

All functions throw on failure. Common errors:

ErrorCause
Key cannot be emptyCheck the error message for details
Invalid key: "...". Must match /^[A-Za-z_][A-Za-z0-9_]*$/Check the error message for details
File path must be a .env file (got: "..."). Use files like .env, .env.local, production.envCheck the error message for details
@desc "Parse and validate result"
do
  set $result as dotenv.parse "KEY=value"
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate

Retrieve all items and loop through them.

@desc "Get and iterate results"
do
  set $result as dotenv.get ".env" "DATABASE_URL"
  each $item in $result
    print $item
  end
enddo

2. Multi-step Dotenv workflow

Chain multiple dotenv operations together.

@desc "Parse, stringify, and more"
do
  set $r_parse as dotenv.parse "KEY=value"
  set $r_stringify as dotenv.stringify $vars
  set $r_load as dotenv.load ".env"
  print "All operations complete"
enddo

3. Safe parse with validation

Check results before proceeding.

@desc "Parse and validate result"
do
  set $result as dotenv.parse "KEY=value"
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/dotenv

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size5.0 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Category

utilities