envalid cleanEnv for typed process env: working versions (tested 2026-10-15)
Validate and coerce environment variables with envalid cleanEnv, str, num, bool and port validators offline.
passed3 passed · 0 failed2026-10-15 20:56 UTC- Status
- passed
- Last passed
- 2026-10-15 20:56 UTC
- Tests
- 3 / 3
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
envalid@8.2.0@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38805962781
The code
Exactly the files that passed. Your agent can pull them with get_recipe("envalid-env-validation") (install), or see them on GitHub.
package.json
{
"name": "envalid-env-validation",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"envalid": "8.2.0"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/env.ts
import { cleanEnv, str, num, bool, port } from "envalid";
export function loadEnv(raw: Record<string, string | undefined>) {
return cleanEnv(raw, {
NODE_ENV: str({ choices: ["development", "test", "production"], default: "test" }),
PORT: port({ default: 3000 }),
MAX_WORKERS: num({ default: 2 }),
FEATURE_X: bool({ default: false }),
API_TOKEN: str({ default: "dev-token" }),
}, {
reporter: ({ errors }) => {
if (Object.keys(errors).length) {
const msg = Object.entries(errors).map(([k, e]) => `${k}: ${(e as Error).message}`).join("; ");
throw new Error(msg);
}
},
});
}
test/env.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { loadEnv } from "../src/env.ts";
test("applies defaults and coerces types", () => {
const env = loadEnv({});
assert.equal(env.NODE_ENV, "test");
assert.equal(env.PORT, 3000);
assert.equal(env.MAX_WORKERS, 2);
assert.equal(env.FEATURE_X, false);
});
test("parses provided values", () => {
const env = loadEnv({
NODE_ENV: "production",
PORT: "8080",
MAX_WORKERS: "8",
FEATURE_X: "true",
API_TOKEN: "secret",
});
assert.equal(env.PORT, 8080);
assert.equal(env.MAX_WORKERS, 8);
assert.equal(env.FEATURE_X, true);
assert.equal(env.API_TOKEN, "secret");
});
test("rejects invalid port", () => {
assert.throws(() => loadEnv({ PORT: "99999" }));
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.