0

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. How can I make node recognise the right file to read? example:

folder-structure:

folder:
   file.js
   .env.local

js file:

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

.env.local file:

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?

RANDOM NAME
  • 41
  • 1
  • 6
  • Why would you use the env variable file from another project? – mars Dec 21 '21 at 17:10
  • How are you loading the env file? – Mureinik Dec 21 '21 at 17:10
  • @mars it's not that i want to use it,it's the program that when i write process.env.NAME_OF_THE_VARIABLE, get's it from another folder containing a .env file instead of the current folder and i don't know why – RANDOM NAME Dec 21 '21 at 17:19
  • @Mureinik i'm not loadining it,i just write process.env.NAME_OF_THE_VARIABLE – RANDOM NAME Dec 21 '21 at 17:20
  • Where in your file structure is the env variable that's being loaded? Are they env variables set up in your Bash profile or something? – mars Dec 21 '21 at 18:11
  • [`dotenv`](https://npmjs.com/package/dotenv) might be a good choice. On the other hand, you can also just change the name of your environment variables so they don't collide. Look at the first answer in https://stackoverflow.com/questions/55406055/toggle-between-multiple-env-files-like-env-development-with-node-js – code Dec 21 '21 at 18:22
  • Does this answer your question? [Toggle between multiple .env files like .env.development with node.js](https://stackoverflow.com/questions/55406055/toggle-between-multiple-env-files-like-env-development-with-node-js) – code Dec 21 '21 at 18:23
  • @code yeah thanks – RANDOM NAME Dec 21 '21 at 19:06

1 Answers1

0

I'd recommend using dotenv to manage your environment variables -- it loades environment variables from a .env file into 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();

Which will allow you to call your env variables like process.env.XXX.

mars
  • 147
  • 12