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

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 command. For example: node app.js, in this case the absolute directory path of app.js). It will return the directory path as a string.

__filename

It will return the file name of the current module. I like to remember it as it returns the absolute file path of the script or js file which we are trying to run using node command. The main difference of __filename and __dirname is that __filename will return the file name along with the directory path which is usually not the case for __dirname.

For example if we run the command , "node app.js", __dirname will give the absolute path of the directory where app.js resides in and __filename will give the absolute path of the directory along with the filename.

process.cwd()

According to the official documentation process.cwd() method returns the current working directory of Node.js process. This basically means that it will return the directory path from where node command is being executed. If we run the node command from the same directory where starting script resides in , then process.cwd() and __dirname will return the same value . If we run the node command from different directory and the script resides in different directory then both of them will return different value.

Dive in to the action 

Here we will study two different scenarios. We will run the node command from the very same directory where our executing script resides in and we will run the node command from a different directory than the executing script.

For this test purpose we will be using the following script which is nothing but outputting the value of __dirname , __filename & process.cwd() to the console. In my case I named it as test.js.

console.log('process.cwd = ' , process.cwd());
console.log('__dirname = ', __dirname);
console.log('__filename = ', __filename);

Now we will run the node command from the same folder where test.js resides in. As we can see in the following screenshot original folder location is "JavascriptProjects/Blogfile/Nodejs" . After running the command "node test.js" we can see that process.pwd() and __dirname returns the same value. Why is that ? because from where the node command is being executed and where the script resides is basically the same folder location. __filename returns the same value of __dirname and also includes the file name of the executing script.


Now we will run the node command from a different location. For this purpose I had created some empty folders. As we can see the current folder location is "Visual Studio Projects/TestCMD" which is different from previous case. From the current folder location we will run the node command and our executing script will be the same as before. We can see in the following screenshot that now process.cwd() returns "Visual Studio Projects/TestCMD" because this is where we run the node command. __dirname returns the folder location of the executing script , which is same as before and __filename returns the folder location of the executing script along with its filename which is also same as before.





Comments

Popular posts from this blog

Electron - How to load/update the current browser window with a new html file?

How to build an apk with React Native