Django + Node.js .env file values by calling are undefined (none)

Hi everyone,

First of all, I would like to thank @KenWhitesell for his contribution to resolve the issues I encountered by building webpages. By his advice, hints given to me I have learned what I had no clue for.

Now, I am using Node.js inside Django framework to get data by API call and render these data by Django on a template. I created a .env file to store credentials and wanted to check if the Node.js reads it. E.g.

// .env
API = "here is my api key"
URL = "here is my url"
USERNAME = "here is my username" etc
// node.js file
require('dotenv").config({path: require("find-config")("./.env")})   // when the file in the same folder where .js file invoked
// I tried this one too
const path = require("path")
// require('dotenv").config({path: path.join(__dirname: "./.env") })  
// require('dotenv").config({path: path.resolve(__dirname + "./.env") })
// require('dotenv").config({path: path.resolve(__dirname + "full_path_to/.env") })
dct = {
    "API_KEY": process.env.API,
   "URL" : process.env.URL

console.log( dct.API_KEY );

Whatever ways I try to check the API on the terminal, I’m getting output undefined though.
I read articles that for Windows10 users in some cases we have to define .env in settings.json of VSCode to enable it. E.g.

"files.associations" : {
   "*.env.*": "env",
   ".env.*": "properties",   // in case you .env file named as *.env.local* etc
   ".env": "env",
"files.include" : {
   "env*": true
"search.include": {
   "**/env": true,

Even providing these settings the node.js did not see / read/ find .env file in VSCode. Although with Python it works without any trouble.

Could someone please tell me what reason the Node.js cannot get the values of .env file inside Django for?

The code works on Chromebook perfectly whereever I store .env file to get desired connection with the server, but not in VSCode on Windows 10.

The link for git repo: django-CloudAppDevelopment_Capstone/functions at master · Kanatbek-AKA/django-CloudAppDevelopment_Capstone · GitHub

I appreciate your feedbacks.

Kind regards,

Can you be more explicit by what you mean when you say:

I’m not familiar with a lot of Node.JS, and I’m not understanding what you’re trying to do here.

Hi @KenWhitesell,

Thanks for your reply.

What I would like to achieve is, inside the Django project there is Node.js used to retrieve data from the IBM Cloudant server by API call. The folder called functions/sample/nodejs & python.
According to the project, I am going to use Django rest-framework to connect with Node.js and render the retrieved information on templates by Django. Hope the main reason for using Node.js in Django is clearly to get the point.

Before to retrieve the data from the server with Node.js, I am checking whether .env file can be found by Node.js to evade any issues with connection, say I am debugging it. As above mentioned, instead Url or Api key I am getting undefined output i.e. Node.js cannot find/ read .env file stored in Django project folder. Although Python reads and returns the api and url that stored in the same path with Node.js - as showed above.

It looks like based on StackOverflow, Dev Community etc., it is a common issue in VSCode on Windows and to fix it there are multiple recommnedations provided. All of them, however, did not work on my device, and am wondering what might be wrong with the codes and settings of VScode I mentioned in my question. Even though this code that you can see on github repo works on Chromebook by both Node.js and Python.

Please let me know if it was clearly elucidated.

The syntax issues I had in git repo were fixed, but the main issue with .env remains.

Kind regards,

So to make sure I’m clear on this, I’m understanding you are describing this situation:

My questions are how are Django and Node.JS communicating with each other? (Question 1 and 2 in the diagram)

So, how specifically is NodeJS trying to read that file? Is it making some type of web request to Django? Or is it trying to read that file directly from the file system?

And where does VSCode come into this picture? I’m not following how this relates to anything else you have mentioned so far.

Side note: Your repo really isn’t of any help because it seems to me to only be a fragment of everything you’re describing here. Specifically, I don’t see your Django project that this is supposed to be communicating with.

From what I do see of this code, I don’t see where Django is involved at all. This appears to be purely a JavaScript problem with trying to read a file from the local file system. Am I missing something here?

1 Like

Indeed, you got that and make it very clear with the schema. :pray: :+1:
Regarding the code stored in git repo are, indeed, some framents then I have not yet started with due to the encountered .env file - undefined.

On Wndows I am working on the project with the VSCode to accomplish the webpage. So the issue with .env file, I thought, might lie in the settings.json of VSCode, but it seems not there. I have no idea now even what is causing reading .env file by Node.js

Node.js is going to remotely retrieve data from the server using credentials stored in .env file and than Django will read these information and render on a template.

Indeed, it will be connected soon as soon I resolve reading the .env file with Node.js, Therefore it is still not implemented.

Thanks for the questions given above. These made my explanation I assume, more just clear.

Ok, but it sounds to me like the issue here is that your NodeJS code is currently unable to read that .env file - which doesn’t seem to be a Django issue at all.

What if you change the name of the file you’re trying to read to something else. Does your code work then? (Instead of trying to read .env, change the name of the file and your code to something like myfile.txt.) I know that’s not the final result that you’re looking for, but it might provide some useful information here.

Also, how are you running your program? What directory are you running it from? (You’re making a relative reference to that file, which isn’t going to work if the current directory isn’t what you think it is.) So another test would be to specify the file name by absolute directory and not a relative reference.

You are right @KenWhitesell, it is not from Djago.

I am going to use without .env file on WIndows to to build the connection with Django.

Thanks for your answers and clarity.

Kind regards,