Node.js '__dirname' vrs 'process.cwd()'
Tags: javascript, node, programming
Published: 2022-09-01 23:37:26

Recently, I have been using Nodejs for almost all my projects, one of the most well-liked backend technologies available today. After a few projects with Nodejs, I soon realized that most developers like myself use to have trouble identifying the subtle difference between two of node's global objects, thus __dirname and process.cwd().

## What is NodeJS?
Node.js is an opensource JavaScript runtime that was built on top of Chrome’s V8 Engine. The traditional JavaScript is executed in browsers but with Node.js we can execute JavaScript on servers, hardware devices, etc.

Now, lets look at what these global variables are and how they differ in their works.

## __dirname:
It is a local variable that returns the name of the current module's directory. It gives back the location of the current JavaScript file's folder.
## process.cwd():
Node.js has got a global object called global, and the process object lies inside the global object. 
process.cwd() gives the name of the directory inside which the NodeJS app is being served from. In other words, the working directory of the NodeJS process.

## Practical Demonstrations
1. In a node Application, I have App.js as my entry file in the root directory
`app.js`

![Example 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tnuc037ci9blx04rj6wh.png)


**Run app.js on the terminal**
```
node app.js
```
**OUTPUT**

```
======== process.cwd()=========
C:\Users\qbentil\blog6
======== __dirname=========
C:\Users\qbentil\blog6
```
2. Create the following project structure

![Structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/njxpj4dny6m8vs94av18.png)

**code snippet in try/try.js**

![try snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvzrr7y4hzlerr1i37g7.png)

**code snippet for app.js**

![app.js snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sl7mrw7cg5aogep6i8dc.png)

```
run node app.js
```
**OUTPUT**

```
======== process.cwd() from /try/try.js=========
C:\Users\User\Desktop\blog6
======== __dirname from /try/try.js=========
c:\Users\User\Desktop\blog6\try
PS C:\Users\User\Desktop\blog6> 
```

The above output shows that the directory of the file try.js is at blog6/try whereas the current node process is running in blog6/ folder.

## Conclusion
Even though the above scope variables, it is important to note the following differences to know which variable to invoke at any point in your application
### Difference  between __dirname and process.cwd()
| __dirname      | process.cwd() |
| ----------- | ----------- |
| It returns the directory name of the directory containing the source code file.| It returns the name the of current working directory where the NodeJS process is being served from.|
|It depends on the current directory.|It depends on the invoking node command.|
| It is local to each module.| It is the node’s global object.    |

As a NodeJS developer, It is very important to know how to use these variables as it can be very confusing sometimes and invoke unnecessary bugs in your app.

**Happy Hacking!**

Bentil here🚀
If you like my content, you can support me here to keep up the work.👇
<a href='https://www.buymeacoffee.com/qbentil' target="_blank">
![buy me a coffee](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jf2285oqld8zpevvsqvt.png)

Let me know your questions or suggestions in the comment box below