Difference between __dirname and ./ in Node.js Last Updated : 08 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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. Understanding the difference between __dirname and ./ is important for file path management in Node.js. __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: javascript // 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); Steps to Run:Open notepad editor and paste the following code and save it with .js extension. For example: index.jsNow open a command prompt and move to the directory where code exists. Type node index.js command to run the code.Output:Example 2: javascript // 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); Steps to Run:Open notepad editor and paste the following code and save it with .js extension. For example: index.jsNow 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: Comment More infoAdvertise with us Next Article Difference between __dirname and ./ in Node.js S Shreyasi_Chakraborty Follow Improve Article Tags : Technical Scripter Web Technologies Node.js Technical Scripter 2019 Node.js-Misc +1 More Similar Reads Difference Between process.cwd() and __dirname in Node.js In Node.js, both process.cwd() and __dirname are used to get the directory paths, but they serve different purposes. The key difference is that process.cwd() returns the current working directory from which the Node.js process was started, while __dirname returns the directory name of the current mo 3 min read Difference between npm install and npm update in Node.js NPM is like a powerhouse for Node.js that contains all the necessary modules for the smooth running of the node.js application. It gets installed on our machine when we install Node.js on our Windows, Linux or MAC OS. How to install Node on the machine? Refer to this article. NPM has 580096 register 5 min read Difference Between Node Require and ES6 Import And Export Importing modules makes your code reusable. NodeJS offers two ways to do this: CommonJS (require()) and ES6 modules (import/export). Both achieve the same goalâsharing code, but use different syntax.CommonJS is the older, traditional way. ES6 modules are newer and offer some advantages, but both are 5 min read Import and Export in Node.js Importing and exporting files are important parts of any programming language. Importing functions or modules enhances the reusability of code. When the application grows in size, maintaining a single file with all the functions and logic becomes difficult. It also hinders the process of debugging. 3 min read How to Create a Directory using Node.js ? In this article, we will create a directory using NodeJS. NodeJS has Filesystem(fs) core module, which enables interacting with the file system, has Node.js fs.mkdir() method or Node.js fs.mkdirSync() method method, to create new directory /parent directory. Prerequisites:Node File SystemThe approac 3 min read Like