简体   繁体   English

有没有办法验证代码中的所有环境变量是否都存在于 .env 文件中?

[英]Is there a way to validate if all environment variables in the code exists in the .env file?

I'm trying to find a way to validate if all of the environment variables used in the code are actually set in the.env file我正在尝试找到一种方法来验证代码中使用的所有环境变量是否实际设置在 .env 文件中

Something like a test to run in the pipeline that throw a warning if I'm missing a variable in the environment I'm currently deploying.如果我在当前部署的环境中缺少变量,则类似于在管道中运行的测试会引发警告。

Context语境

Nodejs节点

It is a big application with a lot of environment variables.这是一个包含大量环境变量的大型应用程序。

There are multiple environments dev, qa, prod.有多个环境 dev、qa、prod。

I'm using the package dotenv我正在使用 package dotenv

dotenv.config()
process.env.A_VARIABLE

There is actually a package targeting exactly this issue: https://github.com/af/envalid实际上有一个 package 正是针对这个问题: https://github.com/af/envalid

You can define a required schema for the .env file;您可以为.env文件定义所需的架构; if a condition is not met, it will end the process with a proper error message.如果不满足条件,它将以适当的错误消息结束该过程。

Furthermore, there are nice features like providing default values and loading a sanitized and typesafe env object for further usage in your application.此外,还有一些不错的功能,例如提供默认值和加载经过清理和类型安全的环境 object 以便在您的应用程序中进一步使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Linux NodeJs.env 文件中的环境变量 - Linux Environment variables in NodeJs .env file 理解 React JS 中的 .env 文件和环境变量 - Understanding .env file and environment variables in React JS 为什么 my.env 文件中的环境变量停止工作? - Why have the environment variables in my .env file stopped working? 有没有更好的方法来调用 .env 文件是否存在检查赛普拉斯中的 function? - Is there a better way to call a .env file exists check function in cypress? dotenv-webpack是否将所有环境变量从.env公开到前端? - Does dotenv-webpack expose all environment variables from .env to front end? 如何列出点 env 文件中列出的所有变量(例如:.env.local.example) - How to list all variables listed in a dot env file (e.g: .env.local.example) node js express 中的环境变量(.env) - Environment variables(.env) in node js express 环境变量未加载到 Nodejs 中的 process.env - Environment Variables not loading to process.env in Nodejs .env 变量在开玩笑测试环境中不可用 - .env variables not available in jest testing environment 如何在不推送 .env-file 的情况下使用环境变量构建 docker-image - How to build docker-image with environment variables while not pushing .env-file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM