no-process-env

禁止使用 process.env

Important

This rule was deprecated in ESLint v7.0.0. Please use the corresponding rule in eslint-plugin-n.

Learn more

Node.js 中的 process.env 对象用于存储部署/配置参数。在整个项目中乱扔垃圾可能会导致维护问题,因为它是另一种全局依赖。因此,它可能导致多用户设置中的合并冲突和多服务器设置中的部署问题。相反,最佳实践之一是将所有这些参数定义在一个可以在整个项目中访问的配置/设置文件中。

¥The process.env object in Node.js is used to store deployment/configuration parameters. Littering it through out a project could lead to maintenance issues as it’s another kind of global dependency. As such, it could lead to merge conflicts in a multi-user setup and deployment issues in a multi-server setup. Instead, one of the best practices is to define all those parameters in a single configuration/settings file which could be accessed throughout the project.

规则详情

¥Rule Details

该规则旨在阻止使用 process.env 以避免全局依赖。因此,无论何时使用 process.env,它都会触发警告。

¥This rule is aimed at discouraging use of process.env to avoid global dependencies. As such, it will warn whenever process.env is used.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint no-process-env: "error"*/

if(process.env.NODE_ENV === "development") {
    //...
}

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*eslint no-process-env: "error"*/

var config = require("./config");

if(config.env === "development") {
    //...
}

何时不使用

¥When Not To Use It

如果你更喜欢在整个项目中使用 process.env 从环境变量中检索值,那么你可以安全地禁用此规则。

¥If you prefer to use process.env throughout your project to retrieve values from environment variables, then you can safely disable this rule.

版本

此规则是在 ESLint v0.9.0 中引入。

进阶读物

资源