eslint-plugin-no-process-env
v1.0.1
Published
ESLint plugin that bans direct process.env access in application code
Downloads
129
Maintainers
Readme
eslint-plugin-no-process-env
Disallow direct process.env access in application code and enforce a centralized environment configuration pattern.
Why would I want this?
Every use of process.env in application code is a dependency on your external environment, and those dependencies are often unsatisfied at runtime, lack validation, and have inconsistent fallbacks.
It's generally a good practice to centralize env var access into a single configuration file which makes them explicit and self-documenting, and creates an opportunity to validate, coerce types, and set default values where appropriate.
This rule won't help your app crash less, but it will help it crash earlier and more loudly!
Install
npm i -D eslint eslint-plugin-no-process-env
yarn add -D eslint eslint-plugin-no-process-env
pnpm add -D eslint eslint-plugin-no-process-env
bun add -d eslint eslint-plugin-no-process-envTested with ESLint 8.57+, 9.x, and 10 alpha. Node 14.17+ for ESLint 8, 18.18+ for ESLint 9, 20.19+ for ESLint 10.
Usage (flat config, ESLint 9+/10)
// eslint.config.mjs
import noProcessEnv from "eslint-plugin-no-process-env";
export default [
{
plugins: { "no-process-env": noProcessEnv },
rules: {
"no-process-env/no-process-env": "error",
},
},
];Usage (eslintrc, ESLint 8)
// .eslintrc.cjs
module.exports = {
plugins: ["no-process-env"],
rules: {
"no-process-env/no-process-env": "error",
},
extends: ["plugin:no-process-env/legacy"],
};The env.ts pattern
Creating a boundary around your environment configuration is a good practice. Using a validation library like Zod makes it even easier to enforce consistent types and fallbacks.
- Create an
env.tsat the root of your app:
// env.ts
import { z } from "zod";
const schema = z.object({
DATABASE_URL: z.string().url(),
NODE_ENV: z.enum(["development", "test", "production"]),
DEBUG_LOGGING_ENABLED: z
.string()
.default("true")
.transform((val) => val !== "false"),
});
export const ENV = schema.parse(process.env);- Import from
env.tselsewhere:
// db/client.ts
import { ENV } from "./env";
const client = new Client({
connectionString: ENV.DATABASE_URL,
});process.envis allowed only insideenv.ts/env.js; everywhere else the rule will error.
What the rule catches
process.env.FOOconst { env } = process;- Bracket access:
process['env']
It ignores:
- Any code inside
env.tsorenv.js. - Shadowed
processidentifiers (e.g.,function process() {}).
Options
None. The rule is purposefully minimal.
Contributing / Development
npm run lint— lint sources and testsnpm run test— run rule tests (Vitest + ESLint RuleTester)npm run build— bundle todist/via TSUP (ESM + d.ts)
The prepare script builds automatically on install from git, which matches npm’s publishing flow.
License
MIT
