Node.js fs.Dir.path() Method

The fs.Dir.path() method is an inbuilt application programming interface of class fs.Dir within File System module which is used to get the path of the directory.

Syntax:

const dir.path

Parameter: This method does not accept any parameter.

Return Value: This method returns the path of the directory.

Below programs illustrates the use of fs.Dir.path() method.



Example 1: Filename: index.js

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the 
// dir.path() method
const fs = require('fs');
  
// Initiating asyn function
async function stop(path) {
  
    // Creating and initiating directory's
    // underlying resource handle
    const dir = await fs.promises.opendir(path);
  
    console.log("All the dirent before operation :- ");
  
    for (var i = 0; i <= 2; i++) {
        console.log(((dir.readSync())).name);
    }
  
    // Getting the path of the directory
    // by using path() method
    const pathval = dir.path;
  
    console.log("All the dirent after operation :- ");
  
    for (var i = 0; i <= 2; i++) {
        console.log(((dir.readSync())).name);
    }
  
    // Display the result
    console.log("\nPath :- " + pathval);
}
  
// Catching error
stop('./').catch(console.error);

chevron_right


Run index.js file using the following command:

node index.js

Output:

All the dirent before operation :- 
books.txt
datastore.json
example.txt
All the dirent after operation :-
index.js
node_modules
package-lock.json

Path :- ./

Example 2: Filename: index.js

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the 
// dir.path() method
const fs = require('fs');
const fsPromises = fs.promises;
  
// Name of the directory to be created
let directo = './temp';
  
// Checking if the directory is
// present or not
if (!fs.existsSync(directo)) {
    fs.mkdirSync(directo);
}
  
// Initiating asyncrionise function
async function funct(path) {
  
    // Initializng dir
    let dir = null;
    let pathval = null;
  
    try {
  
        // Creating and initiating directory's
        // underlying resource handle
        dir = await fs.promises.opendir('./');
  
  
        console.log("All the dirent before operation :- " +
            ((dir.readSync())));
  
        // Getting the path of the directory
        // by using path() method
        pathval = dir.path;
  
    } finally {
  
        if (dir) {
  
            // Close the file if it is opened.
            console.log("Path :- " + pathval);
  
            console.log("All the dirent after operation :- " +
                ((dir.readSync())));
            await dir.close();
        }
    }
}
  
funct('./').catch(console.error);

chevron_right


Run index.js file using the following command:

node index.js

Output:

All the dirent before operation :- [object Object]
Path :- ./
All the dirent after operation :- [object Object]

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_dir_path




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.