Difference between __dirname and ./ in Node.js

Working with any technology requires to interact with files and directories. Files and directories maintain a tree structure for easy access. Working with Node.js also requires accessing files using the file path which can be obtained using different commands. There are two ways of getting the current directory in Node.js . However, they are quite different from each other.

  • __dirname
  • ./

The __dirname in a node script returns the path of the folder where the current JavaScript file resides. __filename and __dirname are used to get the filename and directory name of the currently executing file.

The ./ gives the current working directory. It works similar to process.cwd() method. The current working directory is the path of the folder where the node command executed. However the current working directory may change during the execution of the script by the usage of process.chdir() API.
The only case when ./ gives the path of the currently executing file is when it is used with the require() command which works relative to the current working directory. The ./ allows us import modules based on file structure.



Both __dirname and ./ give similar results when the node is running in the same directory as the currently executing file but produce different results when the node run from some other directory.

__dirname ./
Gives absolute path of the directory that contains the currently executing file. Used to display the path where the terminal is opened, which is the current working directory.
Returns a pointer to a string i.e. the parent directory of currently executing file. Returns a pointer to a string i.e. the current working directory.
Works similar to process.cwd() until the node is run from a directory which is different from the directory where the JavaScript file is stored. Works similar to process.cwd() until used with require() command.

Example: The following examples demonstrate working of __dirname and ./ when node is run from the same directory where the JavaScript file is stored

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// methods to display directory
   
// Include path module
var path = require("path");
  
// Methods to display directory
console.log("__dirname:    ", __dirname);
console.log("process.cwd() : ", process.cwd());
console.log("./ : ", path.resolve("./"));
console.log("filename: ", __filename);

chevron_right


Steps to Run:

  • Open notepad editor and paste the following code and save it with .js extension. For example: index.js
  • Now open a command prompt and move to the directory where code exists.
  • Type node index.js command to run the code.

Output:
image

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// methods to display directory
   
// Include path module
var path = require("path");
  
// Methods to display directory
console.log("__dirname:    ", __dirname);
console.log("process.cwd() : ", process.cwd());
console.log("./ : ", path.resolve("./"));
console.log("filename: ", __filename);

chevron_right


Steps to Run:

  • Open notepad editor and paste the following code and save it with .js extension. For example: index.js
  • Now open a command prompt and if your path is Desktop then type cd.. to move its parent directory.
  • Type node Desktop/index.js command to run the code.

Output:



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.