Skip to content

noUndeclaredEnvVars

¥Summary

¥How to configure

biome.json
{
"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_NAMEimport.meta.env.VAR_NAME 访问,并根据以下内容进行验证:

¥This rule checks for process.env.VAR_NAME and import.meta.env.VAR_NAME accesses and validates them against:

  1. turbo.json(c)globalEnvglobalPassThroughEnv、任务级 env 和任务级 passThroughEnv)中声明的环境变量

    ¥Environment variables declared in turbo.json(c) (globalEnv, globalPassThroughEnv, task-level env, and task-level passThroughEnv)

  2. 在规则的 allowedEnvVars 选项中指定的环境变量

    ¥Environment variables specified in the rule’s allowedEnvVars option

  3. 默认允许的变量(常用系统变量和框架特定模式)

    ¥Default allowed variables (common system vars and framework-specific patterns)

¥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)

  • VERCELVERCEL_* (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 allowed
const value = process.env.NODE_ENV;

¥Options

使用以下选项指定未在 globalEnvglobalPassThroughEnvturbo.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).

biome.json
{
"linter": {
"rules": {
"nursery": {
"noUndeclaredEnvVars": {
"options": {
"allowedEnvVars": [
"MY_APP_.*",
"ACME_TOKEN"
]
}
}
}
}
}
}

¥Related links