Difference between __dirname and ./ in Node.js - GeeksforGeeks

14 Feb, 2020
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.
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:
// Node.js program to demonstrate the// methods to display directory   // Include path modulevar path = require("path");  // Methods to display directoryconsole.log("__dirname:    ", __dirname);console.log("process.cwd() : ", process.cwd());console.log("./ : ", path.resolve("./"));console.log("filename: ", __filename);
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:
Example 2:
// Node.js program to demonstrate the// methods to display directory   // Include path modulevar path = require("path");  // Methods to display directoryconsole.log("__dirname:    ", __dirname);console.log("process.cwd() : ", process.cwd());console.log("./ : ", path.resolve("./"));console.log("filename: ", __filename);
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:
Node.js-Misc
Picked
Technical Scripter 2019
Node.js
Technical Scripter
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
How to read and write Excel file in Node.js ?
Express.js res.render() Function
How to use an ES6 import in Node.js?
Roadmap to Become a Web Developer in 2022
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?
Convert a string to an integer in JavaScript