noConvexProcessEnv (JavaScript)
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/noConvexProcessEnv - This rule has an unsafe fix.
- The default severity of this rule is warning.
- This rule belongs to the following domains:
- Sources:
- Inspired from
@convex-dev/no-process-env
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noConvexProcessEnv": "error" } } }}Description
Section titled “Description”Disallow process.env in Convex functions.
Convex can give you a typed env object for your environment variables. To get it,
declare the variables in convex/convex.config.ts, and then import env from
_generated/server. With env, TypeScript reports misspelled variable names,
tells you which variables are optional, and a deploy fails early when a required
variable is missing. With process.env, every variable has the type
string | undefined, and misspelled names are only found when the code runs.
This rule only checks files inside the convex folder of a package, which is the
folder next to its package.json. Other code, such as frontend code, keeps using
process.env. Files inside the convex/_generated folder are not checked.
The fix replaces process.env with env, and imports env from _generated/server
when the file doesn’t import it yet. The fix is unsafe, because the code only
compiles once the variable is declared in convex/convex.config.ts.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”{ "dependencies": { "convex": "*" }}const apiKey = process.env.GIPHY_KEY;/convex/giphy.ts:1:16 lint/nursery/noConvexProcessEnv FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Don't use process.env in Convex functions.
> 1 │ const apiKey = process.env.GIPHY_KEY;
│ ^^^^^^^^^^^
2 │
ℹ Variables read from process.env have no types, so misspelled or missing variables are only found when the code runs.
ℹ Declare the environment variables in convex/convex.config.ts, and read them from the env object exported by _generated/server.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use the env object from _generated/server instead.
1 │ - const·apiKey·=·process.env.GIPHY_KEY;
1 │ + import·{·env·}·from·"./_generated/server";
2 │ + const·apiKey·=·env.GIPHY_KEY;
2 3 │
const { GIPHY_KEY } = process.env;/convex/keys.ts:1:23 lint/nursery/noConvexProcessEnv FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Don't use process.env in Convex functions.
> 1 │ const { GIPHY_KEY } = process.env;
│ ^^^^^^^^^^^
2 │
ℹ Variables read from process.env have no types, so misspelled or missing variables are only found when the code runs.
ℹ Declare the environment variables in convex/convex.config.ts, and read them from the env object exported by _generated/server.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use the env object from _generated/server instead.
1 │ - const·{·GIPHY_KEY·}·=·process.env;
1 │ + import·{·env·}·from·"./_generated/server";
2 │ + const·{·GIPHY_KEY·}·=·env;
2 3 │
{ "dependencies": { "convex": "*" }}import { env } from "./_generated/server";
const apiKey = env.GIPHY_KEY;The variable is declared in convex/convex.config.ts:
import { defineApp } from "convex/server";import { v } from "convex/values";
const app = defineApp({ env: { GIPHY_KEY: v.string() },});
export default app;See Also
Section titled “See Also”- If you want to disallow
process.envin every file, seenoProcessEnv.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.