by Erisan Olasheni
由Erisan Olasheni
如何为不同的环境阶段自定义Node.js .env文件 (How to customize Node.js .env files for different environment stages)
Have you ever found yourself in a situation where you needed custom environment variables for different development stages of your app? Here is a one-line solution.
您是否曾经遇到过需要在应用程序的不同开发阶段使用自定义环境变量的情况? 这是一线解决方案。
Development has been much easier since the invention of the .env
file. You can easily set your environment variables and values with the syntax ENV_VARIABLE=VALUE
and boom! These variables got loaded as your environment variables, making it possible to get quick access to them:
自发明.env
文件以来,开发工作变得更加容易。 您可以使用语法ENV_VARIABLE=VALUE
轻松设置环境变量和值,然后开始! 这些变量已作为环境变量加载,从而可以快速访问它们:
console.log(process.env.ENV_VARIABLE)
In case you are still wondering what all this means, well, you are probably new to the .env
file. It’s actually a simple configuration text file that is used to define some variables you want to pass into your application’s environment.
如果您仍然想知道这一切意味着什么,那么,您可能是.env
文件的.env
。 它实际上是一个简单的配置文本文件,用于定义一些要传递到应用程序环境中的变量。
This file needs a something like a parser to make it work. The parser reads the variable definitions one-by-one and parses them to the environment. It uses the format ENV_VARIABLE=VALUE (in the case of Node.js: