dev-mastery / comments-api

MIT License
1.92k stars 649 forks source link

.env variable undefined at the time of running data-access module #66

Open Enzo-Parodi opened 3 years ago

Enzo-Parodi commented 3 years ago

Hi @arcdev1, first of all thank you so much for this repo and the lesson about clean architecture on Youtube. I learnt a lot from it and now I'm using both as a guideline for my first own node app.

In data-access/index.js there's something I had to do slightly different because it wasn't working the way you did it, which is here:

In my case, the variable declared in .env (URI in my case) is undefined because (as I found out) the whole data-access module gets executed even before src/index.js does, which contains dotenv.config(),. I solved this by moving URI and the instantiation of MongoClient inside the makeDb function, which gets executed only once the server is started.

I wonder what happened in my case would be expected and if there is any drawback with my solution?

Cheers.

adamcole123 commented 3 years ago

I changed "start" script to this: "start": "npm run clean && npm run build && cd dist && node index.js && npm run db" and it fixed this problem for me. Not sure what other implications this has.