Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/natoboram/load_env
A standalone implementation of Vite's loadEnv
https://github.com/natoboram/load_env
dotenv env environment-variables vite
Last synced: 24 days ago
JSON representation
A standalone implementation of Vite's loadEnv
- Host: GitHub
- URL: https://github.com/natoboram/load_env
- Owner: NatoBoram
- License: lgpl-3.0
- Created: 2024-10-13T04:40:53.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2024-11-15T21:48:43.000Z (about 1 month ago)
- Last Synced: 2024-11-18T22:12:57.097Z (about 1 month ago)
- Topics: dotenv, env, environment-variables, vite
- Language: TypeScript
- Homepage: https://natoboram.github.io/load_env/
- Size: 119 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# `@natoboram/load_env`
[![Node.js CI](https://github.com/NatoBoram/load_env/actions/workflows/node.js.yaml/badge.svg)](https://github.com/NatoBoram/load_env/actions/workflows/node.js.yaml) [![GitHub Pages](https://github.com/NatoBoram/load_env/actions/workflows/github-pages.yaml/badge.svg)](https://github.com/NatoBoram/load_env/actions/workflows/github-pages.yaml) [![Dependabot Updates](https://github.com/NatoBoram/load_env/actions/workflows/dependabot/dependabot-updates/badge.svg)](https://github.com/NatoBoram/load_env/actions/workflows/dependabot/dependabot-updates) [![GitHub Downloads](https://img.shields.io/github/downloads/natoboram/load_env/total?logo=github&color=0969da)](https://github.com/natoboram/load_env/releases) [![NPM Downloads](https://img.shields.io/npm/dt/%40natoboram/load_env?logo=npm&color=CB3837)](https://www.npmjs.com/package/@natoboram/load_env)
A standalone implementation of Vite's `loadEnv`.
## Installation
`@natoboram/load_env` is available on [npmjs](https://www.npmjs.com/package/@natoboram/load_env), [GitHub Packages](https://github.com/NatoBoram/load_env/pkgs/npm/load_env), and [GitHub Releases](https://github.com/NatoBoram/load_env/releases).
```sh
pnpm i @natoboram/load_env
```## Usage
The `loadEnv` function loads environment variables from the current directory's `.env` files. `NODE_ENV` has to be set in the environment and will not be picked up from the filesystem.
If `NODE_ENV` is not set, it defaults to `development`.
Environment variables are loaded in the following order:
1. `.env.${NODE_ENV}.local`
2. `.env.${NODE_ENV}`
3. `.env.local`
4. `.env`Additional functions are provided for the type safety of environment variables. See the [documentation](https://natoboram.github.io/load_env) for the full list of available functions.
```ts
import {
envBool,
envFloat,
envInt,
envString,
envUrl,
envUuid,
loadEnv,
} from "@natoboram/load_env"
import type { UUID } from "crypto"loadEnv()
export const API_URL: URL = envUrl("API_URL")
export const ENABLE_TELEMETRY: boolean = envBool("ENABLE_TELEMETRY")
export const OFFSET: number = envFloat("OFFSET")
export const PORT: number = envInt("PORT")
export const TOKEN: UUID = envUuid("TOKEN")
export const USERNAME: string = envString("USERNAME")
```Note that `envUuid` doesn't actually check if the string is a valid UUID.