Solved: Get current directory in Node.js [4 Examples]


Author: Steve Alila
Reviewer: Deepak Prasad

2 ways you can get current directory details in Node.js

Nodejs get current directory is mainly possible through the __dirname variable


and the process.cwd() method.


It would be best to understand the ins and outs of the __dirname and process.cwd() function before applying them. You will then find it comfortable to Nodejs get current directory especially after going through multiple examples.

First, here is what we mean by Node.js get current directory.


Method-1: Nodejs get current directory path using __dirname

__dirname and __filename are special variables your JavaScript code inherits from the global object's IIFE wrapper.

__dirname prints the current (file's parent) directory's path from the root directory. For example, in Windows, it tells the file path from the /Users directory to the current directory. On the other hand, __dirname shows the current directory's path from the /home directory in Linux.

The (slight) difference between the __dirname and __filename is that __filename attaches the returned path.

Let's create a directory tree to Nodejs get current directory using __dirname and __filename.


Setup lab environment

This section focuses on creating a directory tree in Windows and (Ubuntu) Linux, then reading it using __dirname and __filename.

If you have not, install Node.js on your Linux machine. For Windows, you should install Git Bash besides Node.js to run the Linux commands we are about to use.

Launch the terminal. Create the main directory and a subdirectory with a child directory. Then, create the script file inside the child directory.

$ mkdir GoLinuxCloud
$ cd GoLinuxCloud/
$ mkdir Nodejs_read_current_directory
$ cd Nodejs_read_current_directory/
$ touch script.js


Solved: Get current directory in Node.js [4 Examples]


Solved: Get current directory in Node.js [4 Examples]


Example-1: Using __dirname

Open the script.js file using your preferred code editor, then read the current directory by console-logging the __dirname.

I open the file using vim and enter the following code.


Save the file and exit it before returning to the terminal and running the file with the node command.

node script.js

You should get an output similar to mine.



Nodejs get current directory using __dirname



Solved: Get current directory in Node.js [4 Examples]

We have successfully read the current directory. What if we want to read the current file name as well? That is where __filename comes in.


Example-2: Using __filename

Let's console-log the __filename instead of __dirname


then run the script.

node script.js

This time around, the system appends the file's name to the current directory's name.






Example-3: Exclusively Nodejs get current directory path

Both __dirname and __filename return the current directory's absolute path. But sometimes, all you need is the directory's name, not its full path from the root directory.

You can use the __dirname and path module's basename() method to print the current directory.


const { basename } = require("path");
const currentDirectory = basename(__dirname);

On running the file,

node script.js

we get the following output.



The path module's basename() method returned the last portion of the __dirname variable's output.


Method-2: Nodejs get current directory path using the process.cwd() method

The process object presents us with the cwd() method to print the current working directory. We can Nodejs get current directory by console-logging the method.


Example-4: Using the process object's cwd() method

Let's create a main.js file inside the Nodejs_read_current_directory directory.

touch main.js

Open the file and console-log the current working directory using the process.cwd() method.


Save the file and run it with the node command.

node main.js

We get the output similar to that of console-logging the __dirname.







Applying Nodejs get current directory effectively starts by understanding the relationship between the current directory and the process object, console.log(), __dirname, __filename and the require() method. After that, you can practice reading the current directory, as shown in this tutorial.


Related Keywords: nodejs get current directory, node get current directory, node current directory, nodejs print current directory, nodejs current directory


Views: 44
Steve Alila

Steve Alila

He specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to

Thank You for your support!!

Leave a Comment