https://github.com/mcjkula/lua-dotenv
https://github.com/mcjkula/lua-dotenv
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/mcjkula/lua-dotenv
- Owner: mcjkula
- License: mit
- Created: 2024-09-19T12:39:28.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-09-19T14:07:41.000Z (10 months ago)
- Last Synced: 2025-05-02T19:09:12.852Z (2 months ago)
- Language: Lua
- Size: 7.81 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Lua-Dotenv
lua-dotenv is a simple Lua module that allows you to load environment variables from a .env file into your Lua environment. It provides an easy way to manage configuration in your Lua projects.
## Features
- Load environment variables from a .env file
- Access environment variables with fallback default values
- Simple and lightweight implementation
- Compatible with Lua 5.1 and above## Requirements
- Lua >= 5.1
## Installation
Using LuaRocks:
```luarocks install lua-dotenv```
## Usage
1. Create a .env file:
By default, lua-dotenv looks for the .env file in `~/.config/.env`. You can also specify a custom location when loading the file.
Example .env file content:
```lua
DATABASE_URL=postgresql://user:password@localhost:5432/mydb
DEBUG=true
MAX_CONNECTIONS=100
```2. In your Lua script:
```lua
local dotenv = require("lua-dotenv")
```-- Load variables from default .env file location (`~/.config/.env`)
```lua
dotenv.load_dotenv()
```-- Or, specify a custom .env file location
-- `dotenv.load_dotenv("/path/to/your/.env")`-- Access environment variables
```lua
local db_url = dotenv.get("DATABASE_URL")
local debug_mode = dotenv.get("DEBUG")
local max_conn = dotenv.get("MAX_CONNECTIONS", "50") -- "50" is the default valueprint("Database URL:", db_url)
print("Debug mode:", debug_mode)
print("Max connections:", max_conn)
```## Functions
- `dotenv.load_dotenv(file_path)`: Loads environment variables from the specified file path. If no path is provided, it defaults to ~/.config/.env.
- `dotenv.get(key, default)`: Retrieves the value of the environment variable specified by key. If the variable is not set, it returns the default value.## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.