{"id":15020187,"url":"https://github.com/noahtigner/validate-env-vars","last_synced_at":"2025-06-13T16:02:48.044Z","repository":{"id":212877465,"uuid":"732513670","full_name":"noahtigner/validate-env-vars","owner":"noahtigner","description":"A lightweight utility to check an .env file for the presence and validity of environment variables, as specified via a template file or the command line.","archived":false,"fork":false,"pushed_at":"2025-01-28T00:04:54.000Z","size":948,"stargazers_count":1,"open_issues_count":5,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-31T05:51:18.283Z","etag":null,"topics":["check-env","check-environment","dotenv","env","environment","validate","validate-env","validate-env-vars"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/validate-env-vars","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/noahtigner.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-16T23:22:54.000Z","updated_at":"2024-12-04T05:36:28.000Z","dependencies_parsed_at":"2023-12-17T00:26:51.337Z","dependency_job_id":"a46876d2-898d-48fe-b9f0-c3006fc617dd","html_url":"https://github.com/noahtigner/validate-env-vars","commit_stats":{"total_commits":79,"total_committers":3,"mean_commits":"26.333333333333332","dds":0.3417721518987342,"last_synced_commit":"0a83abe16107ac5af3d2f5ad4cd825f055fa0cda"},"previous_names":["noahtigner/validate-env-vars"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noahtigner%2Fvalidate-env-vars","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noahtigner%2Fvalidate-env-vars/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noahtigner%2Fvalidate-env-vars/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noahtigner%2Fvalidate-env-vars/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/noahtigner","download_url":"https://codeload.github.com/noahtigner/validate-env-vars/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238085306,"owners_count":19414000,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["check-env","check-environment","dotenv","env","environment","validate","validate-env","validate-env-vars"],"created_at":"2024-09-24T19:54:42.224Z","updated_at":"2025-02-10T08:32:11.384Z","avatar_url":"https://github.com/noahtigner.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003evalidate-env-vars\u003c/h1\u003e\n\n\u003cdiv align=\"center\"\u003e\n\n[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/noahtigner/validate-env-vars/blob/HEAD/LICENSE)\n[![latest](https://img.shields.io/npm/v/validate-env-vars/latest.svg)](https://www.npmjs.com/package/validate-env-vars)\n[![last commit](https://img.shields.io/github/last-commit/noahtigner/validate-env-vars.svg)](https://github.com/noahtigner/validate-env-vars/)\n[![npm downloads](https://img.shields.io/npm/dm/validate-env-vars.svg)](https://www.npmjs.com/package/validate-env-vars) \\\n[![Coverage](./badges/coverage.svg)](./badges/coverage.svg)\n[![Code Quality](https://github.com/noahtigner/validate-env-vars/actions/workflows/quality.yml/badge.svg)](https://github.com/noahtigner/validate-env-vars/actions/workflows/quality.yml)\n[![CodeQL](https://github.com/noahtigner/validate-env-vars/actions/workflows/codeql.yml/badge.svg)](https://github.com/noahtigner/validate-env-vars/actions/workflows/codeql.yml)\n\n\u003c/div\u003e\n\n\u003cp align=\"center\"\u003e\n    A lightweight utility to check the presence and validity of environment variables, as specified by a Zod schema\n\u003c/p\u003e\n\n# Installation\n\nUsing npm:\n\n```bash\nnpm install validate-env-vars --save-dev\n```\n\n# Usage Examples\n\n### Create an executable JS file to check an .env file against a Zod schema:\n\n```javascript\n#!/usr/bin/env node\n\nimport validateEnvVars, {\n\tenvEnum,\n\tenvString,\n\tenvNonEmptyString,\n} from 'validate-env-vars';\n\nconst envSchema = envObject({\n\tNODE_ENV: envEnum(['development', 'production', 'test']),\n\tAPI_BASE: envString().url(),\n\tGITHUB_USERNAME: envNonEmptyString(),\n});\n\nvalidateEnvVars({ schema: envSchema });\n```\n\nYou may use the predefined `env*` functions, or create your own using Zod\n\n---\n\n### Programmatically check an .env.production file against a Zod schema:\n\n```javascript\nimport validateEnvVars, {\n    envEnum,\n    envString,\n    envNonEmptyString,\n} from 'validate-env-vars';\n\nconst envSchema = envObject({\n\tNODE_ENV: envEnum(['development', 'production', 'test']),\n\tAPI_BASE: envString().url(),\n\tGITHUB_USERNAME: envNonEmptyString(),\n});\n\nconst prefilight() =\u003e {\n    try {\n        validateEnvVars({ schema: envSchema, envPath: '.env.production' })\n        // ... other code\n    }\n    catch (error) {\n        console.error(error);\n        // ... other code\n    }\n}\n```\n\n---\n\n### Check env vars before Vite startup and build:\n\n1. Define a Zod schema in a .ts file at the root of your project\n\n```javascript\nimport validateEnvVars, {\n    envEnum,\n    envString,\n    envNonEmptyString,\n} from 'validate-env-vars';\n\nconst envSchema = envObject({\n\tNODE_ENV: envEnum(['development', 'production', 'test']),\n\tVITE_API_BASE: envString().url(),\n\tVITE_GITHUB_USERNAME: envNonEmptyString(),\n});\n\n// make the type of the environment variables available globally\ndeclare global {\n    type Env = z.infer\u003ctypeof envSchema\u003e;\n}\n\nexport default envSchema;\n```\n\n2. Import `validateEnvVars` and your schema and add a plugin to your Vite config to call `validateEnvVars` on `buildStart`\n\n```javascript\nimport { defineConfig } from 'vitest/config';\nimport envConfigSchema from './env.config';\nimport validateEnvVars from 'validate-env-vars';\n\nexport default defineConfig({\n  plugins: [\n    {\n      name: 'validate-env-vars',\n      buildStart: () =\u003e validateEnvVars({ schema: envConfigSchema }),\n    },\n    // other plugins...\n  ],\n  // other options...\n```\n\n3. Enable typehints and intellisense for the environment variables in your `vite-env.d.ts`\n\n```javascript\n/// \u003creference types=\"vite/client\" /\u003e\n\ninterface ImportMetaEnv extends globalThis.Env {}\n\ninterface ImportMeta {\n\treadonly env: ImportMetaEnv;\n}\n```\n\n4. Add your schema configuration file to your tsconfig's `include`\n\n# Tips:\n\n- If you don't have a `.env` file, you can pass an empty file. This is useful for testing and CI/CD environments, where environment variables may be set programmatically.\n\n# Config Options\n\n| Option                   | Type        | Description                                                    | Default |\n| ------------------------ | ----------- | -------------------------------------------------------------- | ------- |\n| `schema`                 | `EnvObject` | The schema to validate against                                 |         |\n| `envPath` (optional)     | `string`    | The path to the .env file                                      | `.env`  |\n| `exitOnError` (optional) | `boolean`   | Whether to exit the process or throw if validation fails       | `false` |\n| `logVars` (optional)     | `boolean`   | Whether to output successfully parsed variables to the console | `true`  |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoahtigner%2Fvalidate-env-vars","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnoahtigner%2Fvalidate-env-vars","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoahtigner%2Fvalidate-env-vars/lists"}