npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@stacklance/envguard-audit

v0.1.0

Published

Static analysis tool that audits process.env usage against a Zod schema

Readme

@stacklance/envguard-audit

Static analysis tool that scans your codebase for process.env usage and audits it against your Zod schema. Catches undeclared env vars, unused schema keys, and dynamic access patterns — before they hit production.

The Problem

Your schema declares PORT, DB_URL, and NODE_ENV. But somewhere deep in src/utils/cache.ts, someone wrote process.env.REDIS_URL — and it's not in the schema. Your app boots fine in dev (the variable happens to be set), but fails in staging. @stacklance/envguard-audit catches this at lint time.

Installation

npm install @stacklance/envguard-audit

CLI Usage

The audit command is available via @stacklance/envguard-cli:

# Basic audit
env-guard audit --dir ./src --schema ./env.schema.ts

# Scan multiple directories (monorepo)
env-guard audit --dir ./apps/api ./apps/web --schema ./env.schema.ts

# Auto-fix: add undeclared keys to schema
env-guard audit --dir ./src --schema ./env.schema.ts --fix

# JSON output for tooling integration
env-guard audit --dir ./src --schema ./env.schema.ts --json

Programmatic API

import { audit } from '@stacklance/envguard-audit';

const result = await audit({
  dir: './src',
  schema: './env.schema.ts',
});

console.log(result.undeclared); // { key, file, line }[]
console.log(result.unused);    // string[]
console.log(result.unsafe);    // { expression, file, line }[]

What It Detects

Undeclared (result.undeclared)

Env vars used in code but not declared in the schema:

// env.schema.ts declares: PORT, DB_URL
// but in src/cache.ts:
const redis = process.env.REDIS_URL; // ← UNDECLARED

Unused (result.unused)

Schema keys that are never referenced anywhere in the scanned code:

// env.schema.ts declares: PORT, DB_URL, LEGACY_FLAG
// LEGACY_FLAG is never used in any source file → UNUSED

Unsafe / Dynamic Access (result.unsafe)

Dynamic process.env[variable] patterns that cannot be statically resolved. These are flagged as DYNAMIC_ACCESS for manual review — zero false positives on intentional dynamic access:

const key = getConfigKey();
const val = process.env[key]; // ← DYNAMIC_ACCESS: needs manual review

Ignoring Specific Lines

Add // envguard-ignore on the same line to skip an access:

const legacy = process.env.OLD_FLAG; // envguard-ignore
const dyn = process.env[computed];   // envguard-ignore

CI Integration with GitHub Actions

# .github/workflows/ci.yml
jobs:
  audit-env:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'pnpm'
      - run: pnpm install --frozen-lockfile
      - run: npx env-guard audit --dir ./src --schema ./env.schema.ts

When CI=true, the CLI automatically outputs GitHub Actions annotations:

::error file=src/utils/db.ts,line=14::UNDECLARED env var "REDIS_URL" is not in the schema
::warning file=src/config.ts,line=8::DYNAMIC_ACCESS process.env[key] cannot be statically verified
::warning::UNUSED schema key "LEGACY_FLAG" is never referenced in code

These show up as inline annotations on pull requests.

--fix Mode

Automatically adds undeclared keys to your schema file as z.string().optional():

env-guard audit --dir ./src --schema ./env.schema.ts --fix

Before:

export default {
  PORT: z.coerce.number().default(3000),
  DB_URL: z.string().url(),
};

After:

export default {
  PORT: z.coerce.number().default(3000),
  DB_URL: z.string().url(),
  REDIS_URL: z.string().optional(),
  API_SECRET: z.string().optional(),
};

JSON Output

Use --json for machine-readable output, ideal for integration with other tools:

{
  "undeclared": [
    { "key": "REDIS_URL", "file": "src/utils/db.ts", "line": 14 }
  ],
  "unused": ["LEGACY_FLAG"],
  "unsafe": [
    { "expression": "key", "file": "src/config.ts", "line": 8 }
  ],
  "summary": {
    "undeclared": 1,
    "unused": 1,
    "unsafe": 1
  }
}

VS Code Terminal

Run the audit directly in the VS Code integrated terminal for clickable file:line links:

npx env-guard audit --dir ./src --schema ./env.schema.ts

Output includes file paths and line numbers that VS Code makes clickable:

✖ Undeclared env vars (1):
  ● REDIS_URL  src/utils/db.ts:14

⚠ Unused schema keys (1):
  ● LEGACY_FLAG

⚡ Dynamic access — manual review needed (1):
  ● process.env[key]  src/config.ts:8

Exit Codes

| Code | Meaning | | ---- | ------- | | 0 | All process.env accesses match the schema | | 1 | Undeclared env vars found (CI-friendly fail) |

Note: unused keys and dynamic accesses are warnings — they don't cause a non-zero exit.

License

MIT