1

I am tryng to implement a simple REST API with Node Express, but I am not able to put the env file to work in it. There is an .env file in the repository, with the url to the remote database and a couple of other data that are necessary to the project, but is better if they remain hided. Here there is a sample of the dotenv file:

CONNECTION_URL = www.blablabla.com
PASSWORD = admin
PORT = 5000

But it is never opened. In the main code, I wrote as usually the following line to allow it to read and parse the file as needed:

// importing environment variables
require('dotenv').config();

But is seems to not work, in fact I cannot connect to the remote database and choose what port to listend on, and some other stats and variable are not usable. What should I do?

There are two thing to note that I don't think are relevant: this is a Typescript project per se, and I put the .env file into the .gitignore, but as it is present in local, it should not be an issue at all.

I read some stackoverflow question, like the following: dotenv file is not loading environment variables

but this did not solve my problem. It didn't change anything. I also tried to update dotenv and everything.

Nothing else seemed to work properly. Thank you all for your help

EDIT I tried to pass statically the link itself, and it works properly, so the issue is the env file.

I tried also to console.log the process.ENV.CONNECTION_URL, and it returned undefined. It is that for sure

Matteo Possamai
  • 455
  • 1
  • 10
  • 23
  • Could You provide link to the repo Please ? – MarioG8 Nov 13 '22 at 09:55
  • Sure, https://github.com/MatteoPossamai/football_visualizer – Matteo Possamai Nov 13 '22 at 10:03
  • Could You show us the terminal error msg please ? – MarioG8 Nov 13 '22 at 10:27
  • ```ndefined Server running on port 3000 could not connect to postgres Error: connect ECONNREFUSED 127.0.0.1:5432 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16) { errno: -111, code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 5432 }``` It is the message of error of connection because it cannot reach any db because it does not have the link. Please read the edit I wrote in the question – Matteo Possamai Nov 13 '22 at 10:41
  • Did You Check your database connection configuration and credentials exactly ? – MarioG8 Nov 13 '22 at 11:00
  • Yes, if is statical it works – Matteo Possamai Nov 13 '22 at 11:17
  • Where exactly did you place your `.env` file? Docs say "in the root of the project" (ie where your package.json is), but that seems to work only if you also start the project from the root of the project. – derpirscher Nov 13 '22 at 12:12
  • What is your CWD, how do your start the app? Where is your "index" file? Post absolute paths. My guess is you have a problem with the working directory/path of the .env file. – Marc Nov 13 '22 at 12:21

0 Answers0