Node.js ‘__dirname’ vrs ‘process.cwd()’

nodejs-‘-dirname’-vrs-‘process.cwd()’

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

Run app.js on the terminal

node app.js

OUTPUT

======== process.cwd()=========
C:Usersqbentilblog6
======== __dirname=========
C:Usersqbentilblog6
  1. Create the following project structure

Structure

code snippet in try/try.js

try snippet

code snippet for app.js

app.js snippet

run node app.js

OUTPUT

======== process.cwd() from /try/try.js=========
C:UsersUserDesktopblog6
======== __dirname from /try/try.js=========
c:UsersUserDesktopblog6try
PS C:UsersUserDesktopblog6> 

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.👇

buy me a coffee

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

Total
18
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
xrp-ledger-sidechains,-redesigned

XRP Ledger Sidechains, Redesigned

Next Post
what-are-arrays?-–-part-iv

What are Arrays? – Part IV

Related Posts