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


NodeJS

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

console.log(__dirname)

and the process.cwd() method.

console.log(process.cwd())

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

Linux:

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

Windows:

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.

console.log(__dirname)

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.

Linux:

/home/<username>/GoLinuxCloud/Nodejs_read_current_directory

Nodejs get current directory using __dirname

Windows:

C:\Users<username>\GoLinuxCloud\Nodejs_read_current_directory

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

console.log(__filename)

then run the script.

node script.js

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

Linux:

/home/<username>/GoLinuxCloud/Nodejs_read_current_directory/script.js

Windows:

C:\Users<username>\GoLinuxCloud\Nodejs_read_current_directory\script.js

 

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.

Input:

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

On running the file,

node script.js

we get the following output.

Output:

Nodejs_read_current_directory

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.

console.log(process.cwd())

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.

Linux:

/home/<username>/GoLinuxCloud/Nodejs_read_current_directory

Windows:

C:\Users<username>\GoLinuxCloud\Nodejs_read_current_directory

 

Conclusion

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

 

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 admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment