Posts

Showing posts from July, 2020

Difference between __dirname vs __filename vs process.cwd() : once and for all

Image
Recently I started working with nodejs, one of the most popular backend technologies in today's era. After spending some time with nodejs , I quickly realised that I am facing some difficulties figuring out the tiny differences among three of node's global objects, i.e __dirname, __filename and process.cwd . The main reason of causing this confusion is that they are closely related to each other and behave almost equally in certain scenarios. That's why I sat down today to clear the confusion and figure out the differences . This post is about writing down all those details for future references and helping out people who are in the same shoe now, in the past or in the near future. At first lets see what the official documentation of node says about these three global objects. __dirname It will return the directory name of the current module. I like to remember it as it returns the absolute directory path of the script or js file (the one which we run using node com...