process.cwd vs __dirname in Node.js

Sometimes you want to know where you are. This happens plenty of times in real life, but it also happens in Node.js. If you need to programatically determine the current folder, you may turn to process.cwd() or __dirname. There is a difference between them though. In this article we'll discuss difference between the process.cwd() method and __dirname keyword.

To get started, clone the following git hub repository:

https://github.com/kevinchisholm/process.cwd-vs-__dirname-in-node.js

(Instructions on how to run the code are available in the Git hub page.)

index.js

Example # 1:

let tools = require('./tools/tools');
let subTools = require('./tools/subTools/subTools');
 
//show directories (A)
console.log('A: process.cwd() -> ' + process.cwd());
console.log('A: __dirname -> ' + __dirname);
 
//show directories (B)
tools.showDirectories();
 
//show directories (C)
subTools.showDirectories();

Run the example code in your terminal with the following command: node index.js.

The resulting output in your terminal is:

A: process.cwd() -> YOUR_PATH/process.cwd-vs-__dirname-in-node.js
 
A: __dirname -> YOUR_PATH/process.cwd-vs-__dirname-in-node.js
 
B -> process.cwd() -> YOUR_PATH/process.cwd-vs-__dirname-in-node.js
 
B -> __dirname -> YOUR_PATH/process.cwd-vs-__dirname-in-node.js/tools
 
C -> process.cwd() -> YOUR_PATH/process.cwd-vs-__dirname-in-node.js
 
C -> __dirname -> YOUR_PATH/process.cwd-vs-__dirname-in-node.js/tools/subTools

(NOTE: YOUR_PATH represents the actual path on your computer. Yours will not be exactly the same as mine)

In example # 1, we have the contents of index.js. The first two console log statements should have the same output (section A). The reason for this is: we are in the root directory of the project. The process.cwd method shows the absolute path. But in this case, the __dirname keyword does as well because the script is running in the root directory of the project.

The third and fourth console.log statements (section B), call the tools.showDirectories method. The 5th and 6thconsole.log statements (section C), call the subTools.showDirectories method. Let's look at section B next.

tools/tools

Example # 2:

module.exports.showDirectories = function () {
 
    //show directories (B)
    console.log('B -> process.cwd() -> ' + process.cwd());
    console.log('B -> __dirname -> ' + __dirname);
};

In example # 2, we take the exact same steps as section A. But, the output is a little different. The fist console.log statement is the same as section A because the process.cwd method will always show the absolute path. The output of the __dirname keyword differs from section A because the directory that we call it from is different. Now we are in the tools folder, so the output includes /tools in the directory name.

tools/subTools/subTools

Example # 3:

module.exports.showDirectories = function () {
 
    //show directories (C)
    console.log('C -> process.cwd() -> ' + process.cwd());
    console.log('C -> __dirname -> ' + __dirname);
};

In example # 3, we once again take the exact same steps as section A. The fist console.log statement is the same as section A. The output of the __dirname keyword differs from sections A and B because the directory that we call it from is different. Now we are in the /tools/subTools folder, so the output includes /tools/subTools in the directory name.

Summary

In this article, we learned about the difference between process.cwd() and __dirname in node.js. Both methods are useful. But depending on the situation, one may be more appropriate than the other at times.

Author

Kevin Chisholm

http://blog.kevinchisholm.com

Share This