kotlin-kotenv

Loads .env files into runtime via DSL or fluent builder, honoring system env precedence; supports quoted/multiline values, comments, escapes, duplicates, and ignore options.

JVMKotlin/NativeWasmJS
GitHub stars0
AuthorsPaoloConte
Open issues0
LicenseMIT License
Creation date2 months ago

Last activity2 months ago
Latest release1.0.2 (2 months ago)

Kotenv

A Kotlin Multiplatform library for loading environment variables from .env files. Inspired also from dotenv-kotlin and dotenv-java, but with multi-platform support.

Supported Platforms

  • JVM
  • JS (Node.js)
  • WasmJS (Node.js)
  • Native (macOS, Linux, Windows)

Install

implementation("io.paoloconte:kotenv:1.0.2")

Usage

DSL Builder

val env = kotenv {
    directory = Path("path", "to", "directory")
    filename = "prod.env"
    ignoreIfMissing = true
    ignoreIfMalformed = true
    ignoreIfEmpty = true
}

val value = env["MY_VARIABLE"] ?: "default"

Fluent Builder

val env = Kotenv.builder()
    .directory(Path("path", "to", "directory"))
    .filename("dev.env")
    .ignoreIfMissing()
    .ignoreIfMalformed()
    .ignoreIfEmpty()
    .build()

val value = env["MY_VARIABLE"] ?: "default"

The directory parameter uses kotlinx.io.files.Path. Use the vararg constructor to build platform-independent paths:

import kotlinx.io.files.Path

Options

Option Default Description
filename .env The name of the environment file to load
directory current The directory where the environment file is located
ignoreIfMissing false If true, no exception is thrown when the file is not found
ignoreIfMalformed false If true, malformed lines are silently ignored instead of throwing an exception
ignoreIfEmpty false If true, variables with empty values (e.g., VAR=) are excluded and return null; Also applies to malformed

Features

  • System environment variables take priority over .env file values
  • Supports quoted values with multi-line strings
  • Supports comments (#) and trailing comments
  • Supports escape sequences in quoted values (\n, \t, \r, \", \\)
  • Duplicate keys: last value wins

.env File Format

# Comment
SIMPLE_VAR=value
QUOTED_VAR="value with spaces"
MULTI_LINE="line1
line2"
EMPTY_VAR=
TRAILING_COMMENT=value # this is ignored
JVMKotlin/NativeWasmJS
GitHub stars0
AuthorsPaoloConte
Open issues0
LicenseMIT License
Creation date2 months ago

Last activity2 months ago
Latest release1.0.2 (2 months ago)

Kotenv

A Kotlin Multiplatform library for loading environment variables from .env files. Inspired also from dotenv-kotlin and dotenv-java, but with multi-platform support.

Supported Platforms

  • JVM
  • JS (Node.js)
  • WasmJS (Node.js)
  • Native (macOS, Linux, Windows)

Install

implementation("io.paoloconte:kotenv:1.0.2")

Usage

DSL Builder

val env = kotenv {
    directory = Path("path", "to", "directory")
    filename = "prod.env"
    ignoreIfMissing = true
    ignoreIfMalformed = true
    ignoreIfEmpty = true
}

val value = env["MY_VARIABLE"] ?: "default"

Fluent Builder

val env = Kotenv.builder()
    .directory(Path("path", "to", "directory"))
    .filename("dev.env")
    .ignoreIfMissing()
    .ignoreIfMalformed()
    .ignoreIfEmpty()
    .build()

val value = env["MY_VARIABLE"] ?: "default"

The directory parameter uses kotlinx.io.files.Path. Use the vararg constructor to build platform-independent paths:

import kotlinx.io.files.Path

Options

Option Default Description
filename .env The name of the environment file to load
directory current The directory where the environment file is located
ignoreIfMissing false If true, no exception is thrown when the file is not found
ignoreIfMalformed false If true, malformed lines are silently ignored instead of throwing an exception
ignoreIfEmpty false If true, variables with empty values (e.g., VAR=) are excluded and return null; Also applies to malformed

Features

  • System environment variables take priority over .env file values
  • Supports quoted values with multi-line strings
  • Supports comments (#) and trailing comments
  • Supports escape sequences in quoted values (\n, \t, \r, \", \\)
  • Duplicate keys: last value wins

.env File Format

# Comment
SIMPLE_VAR=value
QUOTED_VAR="value with spaces"
MULTI_LINE="line1
line2"
EMPTY_VAR=
TRAILING_COMMENT=value # this is ignored
Survey iconComplete survey to improve klibs.io ↗
Let’s go