简体   繁体   English

如何避免从 process.env 文件中获取错误数据?

[英]How to avoid getting wrong data from process.env file?

I need to get some data from a.env.local file,however instead of getting the data of the file on the current folder i get the one from another project that i had made.我需要从 a.env.local 文件中获取一些数据,但是我没有从当前文件夹中获取文件的数据,而是从我制作的另一个项目中获取数据。 How can I make node recognise the right file to read?如何让节点识别要读取的正确文件? example:例子:

folder-structure:文件夹结构:

folder:
   file.js
   .env.local

js file: .js 文件:

   const envData ={
      test1: process.env.TEST_1,
      test2: process.env.TEST_2,
      test3: process.env.TEST_3,
    })

.env.local file: .env.local 文件:

TEST_1=test1-data
TEST_2=test2-data
TEST_3=test3-data

The problem is:I don't get the data from the env file in my folder but from another one,how do I fix this?问题是:我没有从文件夹中的 env 文件中获取数据,而是从另一个文件夹中获取数据,我该如何解决这个问题?

I'd recommend using dotenv to manage your environment variables -- it loades environment variables from a .env file into process.env .我建议使用dotenv来管理您的环境变量——它将环境变量从.env文件加载到process.env中。 So you'll have your .env file within your project folder and then whichever folder you need to load your env variables in, you'll require and configure the package at the top of the file like so: require('dotenv').config();因此,您将在项目文件夹中拥有.env文件,然后在需要加载 env 变量的任何文件夹中,您将需要并在文件顶部配置 package,如下所示: require('dotenv').config();

Which will allow you to call your env variables like process.env.XXX .这将允许您调用您的环境变量,例如process.env.XXX

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM