{"id":15204114,"url":"https://github.com/hazlema/setenv","last_synced_at":"2026-02-19T17:01:18.922Z","repository":{"id":257647913,"uuid":"858487956","full_name":"hazlema/setEnv","owner":"hazlema","description":"A lightweight, configurable .env file loader for Bun applications in just 43 lines of TypeScript.","archived":false,"fork":false,"pushed_at":"2024-09-17T18:49:00.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-19T09:52:54.152Z","etag":null,"topics":["bun","env","enviroment","typescripe"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hazlema.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-17T01:35:11.000Z","updated_at":"2024-09-17T19:04:49.000Z","dependencies_parsed_at":"2024-09-17T23:14:41.965Z","dependency_job_id":"4b1415af-7772-45d1-a16d-886115bd3eef","html_url":"https://github.com/hazlema/setEnv","commit_stats":null,"previous_names":["hazlema/setenv"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/hazlema/setEnv","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazlema%2FsetEnv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazlema%2FsetEnv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazlema%2FsetEnv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazlema%2FsetEnv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hazlema","download_url":"https://codeload.github.com/hazlema/setEnv/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazlema%2FsetEnv/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29623546,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T13:04:20.082Z","status":"ssl_error","status_checked_at":"2026-02-19T13:03:33.775Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bun","env","enviroment","typescripe"],"created_at":"2024-09-28T05:21:33.095Z","updated_at":"2026-02-19T17:01:18.906Z","avatar_url":"https://github.com/hazlema.png","language":"TypeScript","readme":"# Lightweight .env File Loader for Bun\n\n`setEnv` is a simple, efficient function for loading environment variables from a `.env` file in Bun applications. It's designed to be a lightweight alternative to full-featured dotenv libraries, perfect for projects that need basic .env functionality without additional overhead.\n\n## Features\n\n- Lightweight: Just 43 lines of code\n- Configurable: Supports custom .env file paths\n- Efficient: Uses Bun's native file APIs and Node.js streams for optimal performance\n- TypeScript support: Fully typed for improved developer experience\n- Cross-platform compatible: Handles different line ending styles (CRLF, LF, CR)\n- Comment support: Ignores lines starting with #\n- Handles complex values: Correctly processes values containing = characters\n- Variable expansion: Supports referencing other environment variables using $VARIABLE_NAME syntax\n\n## Installation\n\nNo installation required! Just copy the `setEnv` function into your project.\n\n## Usage\n\n1. Copy the `setEnv` function into your project, for example in a file named `setEnv.ts`:\n\n```typescript\nimport { createReadStream } from \"node:fs\";\nimport { createInterface } from \"node:readline\";\nimport { Readable } from \"node:stream\";\nimport type { BunFile } from \"bun\";\n\nconst setEnv = async (envPath: string = \".env\"): Promise\u003cvoid\u003e =\u003e {\n  const file: BunFile = Bun.file(envPath);\n\n  if (file.name \u0026\u0026 (await file.exists())) {\n    const fileStream: Readable = createReadStream(file.name);\n\n    const rl = createInterface({\n      input: fileStream,\n      crlfDelay: Infinity,\n    });\n\n    for await (const line of rl) {\n      if (line.startsWith(\"#\") || line.trim() === \"\") continue;\n\n      if (line.includes(\"=\")) {\n        let [key, ...valueParts] = line.split(\"=\");\n        let value = valueParts.join(\"=\").trim();\n\n        key = key.trim();\n\n        Object.keys(process.env).forEach((envKey) =\u003e {\n          if (process.env[envKey]) {\n            const placeholder: string = \"$\" + envKey;\n\n            if (value.includes(placeholder)) {\n              value = value.replace(placeholder, process.env[envKey] as string);\n            }\n          }\n        });\n\n        if (key \u0026\u0026 value) {\n          process.env[key] = value;\n        }\n      }\n    }\n  }\n};\n\nexport default setEnv;\n```\n\n2. In your main application file, import and call `setEnv`:\n\n```typescript\nimport setEnv from './setEnv'\nawait setEnv() // Loads from default .env file\n```\n\n3. Create a `.env` file in your project root (or at the specified path):\n\n```\n# This is a comment\nKEY1=value1\nKEY2=value with = sign\nBASE_URL=$HOST:$PORT\n```\n\n4. Run your Bun application as usual.\n\n## How It Works\n\n1. The function checks for the existence of the specified .env file (defaulting to `.env` in the current directory).\n2. If the file exists, it's read line by line using Node.js streams for efficiency.\n3. Each line is processed:\n   - Comments (lines starting with #) and empty lines are ignored.\n   - Key-value pairs are split on the first = character.\n   - Values are trimmed of whitespace.\n   - Variable expansion is performed, replacing $VARIABLE_NAME with the corresponding environment variable value.\n   - The resulting key-value pairs are added to `process.env`.\n\n## Advanced Usage\n\nYou can use `setEnv` with different environment configurations:\n\n```typescript\n// Load development environment\nawait setEnv('.env.development')\n\n// Load production environment\nawait setEnv('.env.production')\n\n// Load test environment\nawait setEnv('.env.test')\n```\n\nThis flexibility allows you to manage multiple environment configurations easily.\n\n## Variable Expansion\n\nThe `setEnv` function now supports variable expansion. You can reference other environment variables in your `.env` file using the `$VARIABLE_NAME` syntax. For example:\n\n```\nHOST=localhost\nPORT=3000\nBASE_URL=$HOST:$PORT\n```\n\nIn this case, `BASE_URL` will be set to `localhost:3000`.\n\n## Limitations\n\n- Does not include options to prevent overwriting existing environment variables\n\n## Contributing\n\nFeel free to submit issues or pull requests if you have suggestions for improvements or encounter any problems.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhazlema%2Fsetenv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhazlema%2Fsetenv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhazlema%2Fsetenv/lists"}