noUndeclaredEnvVars
¥Summary
-
规则生效日期:
v2.3.10¥Rule available since:
v2.3.10 -
诊断类别:
lint/nursery/noUndeclaredEnvVars¥Diagnostic Category:
lint/nursery/noUndeclaredEnvVars -
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
-
此规则属于以下域:
¥This rule belongs to the following domains:
-
来源:
¥Sources:
-
与
turbo/no-undeclared-env-vars相同¥Same as
turbo/no-undeclared-env-vars
-
¥How to configure
{ "linter": { "rules": { "nursery": { "noUndeclaredEnvVars": "error" } } }}¥Description
禁止使用未声明的环境变量。
¥Disallow the use of undeclared environment variables.
在 Turborepo 项目中,任务中使用的环境变量必须在 turbo.json(c) 配置文件中声明,以确保正确的缓存行为。使用未声明的环境变量可能会导致缓存命中错误和构建行为不可预测。
¥In Turborepo projects, environment variables used in tasks must be declared
in the turbo.json(c) configuration file to ensure proper caching behavior.
Using undeclared environment variables can lead to incorrect cache hits
and unpredictable build behavior.
此规则检查 process.env.VAR_NAME 和 import.meta.env.VAR_NAME 访问,并根据以下内容进行验证:
¥This rule checks for process.env.VAR_NAME and import.meta.env.VAR_NAME
accesses and validates them against:
-
在
turbo.json(c)(globalEnv、globalPassThroughEnv、任务级env和任务级passThroughEnv)中声明的环境变量¥Environment variables declared in
turbo.json(c)(globalEnv,globalPassThroughEnv, task-levelenv, and task-levelpassThroughEnv) -
在规则的
allowedEnvVars选项中指定的环境变量¥Environment variables specified in the rule’s
allowedEnvVarsoption -
默认允许的变量(常用系统变量和框架特定模式)
¥Default allowed variables (common system vars and framework-specific patterns)
默认允许变量
Section titled “默认允许变量”¥Default Allowed Variables
以下环境变量始终允许使用,无需显式声明:
¥The following environment variables are always allowed without explicit declaration:
系统变量:
¥System variables:
CI,HOME,PATH,PWD,SHELL,TZ,USER
Node.js:
NODE_ENV
框架和提供程序特定模式(所有匹配这些前缀的变量):
¥Framework and provider-specific patterns (all variables matching these prefixes):
-
NEXT_PUBLIC_*(Next.js) -
VITE_*(Vite) -
REACT_APP_*(Create React App) -
VUE_APP_*(Vue CLI) -
NUXT_*(Nuxt) -
GATSBY_*(Gatsby) -
EXPO_PUBLIC_*(Expo) -
VERCEL、VERCEL_*(Vercel)
¥Examples
¥Invalid
当 MY_VAR 未在 turbo.json 或允许列表中声明时:
¥When MY_VAR is not declared in turbo.json or the allowed list:
const value = process.env.MY_VAR;¥Valid
// NODE_ENV is always allowedconst value = process.env.NODE_ENV;¥Options
使用以下选项指定未在 globalEnv、globalPassThroughEnv 或 turbo.json 中的任务级 env/passThroughEnv 中声明的其他环境变量。支持正则表达式模式(锚点 ^ 和 $ 是隐式的)。
¥Use the options to specify additional environment variables that are not declared in globalEnv,
globalPassThroughEnv, or task-level env/passThroughEnv in turbo.json.
Supports regular expression patterns (anchors ^ and $ are implicit).
{ "linter": { "rules": { "nursery": { "noUndeclaredEnvVars": { "options": { "allowedEnvVars": [ "MY_APP_.*", "ACME_TOKEN" ] } } } } }}¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号