Getting Started With The Filepath Node Js Module

Sometimes, you need to know where you are. To be specific; sometimes, your Node.js application needs to know things about the folder in which it is. In this article, we will learn how to solve this problem using the filepath node module.

To get started, clone the following git hub repository:

https://github.com/kevinchisholm/getting-started-with-the-filepath-nodejs-module

(Instructions on how to run the code are available in the Git hub page.)

package.json

{
  "dependencies": {
    "filepath": "1.1.0"
  }
}

The package.json for this project is simple: our single dependency is the filepath node module.

Determining the path to the current folder

Example # 1:

//get a reference to the filepath module
var fp = require('filepath');

//get a reference to the folder structure that leads up to the current file, 
//set it to the path variable
var path = fp.newPath();

//output the path variable 
console.log(path.toString());

In example # 1, we set a reference to the filepath node.js module. The, we use the path variable as a reference for the path to the current folder. We called the newPath method of the fp instance. In our console.log statement, we then output the path to the current folder using the toString method of the path variable.

Run example # 1 in your terminal with the following command: node example-1.js.

The result of this is: [YOUR_PATH]/getting-started-with-the-filepath-nodejs-module*

(NOTE: [YOUR_PATH] represents the actual path to this folder on your computer, which will vary)

Determining the name of every file in the current folder

Example # 2:

//get a reference to the filepath module
var fp = require('filepath'),
    //get a reference to the folder structure that leads up to the current file, 
    //set it to the path variable
    path = fp.newPath(),
    //get a reference to a list of all files in the current folder
    files = path.list();

//for each file name in the files array
files.forEach(function (fileName) {
    //output the full path to this file
    console.log(fileName.toString());
});

In example # 2 we have the new files variable. We have executed the list method of the path object. Since it is an array, we can use the forEach method to iterate over that array. For each element in the array, we get a file object. When then call that objects toString method. The result is a list of every file in the current folder.

Recursively determining the name of every file and folder in the current folder

Example # 3:

//get a reference to the filepath module
var fp = require('filepath');

//list all files in all folders in the current folder
//and recursively do the same for any sub-folders
fp.newPath(__dirname).recurse(function (path) {
    console.log(path.toString());
})

In example # 3 our code is simpler. Like the previous examples, we have the fp variable, which is an instance of the filepath module. We then use that module's newPath method. This method takes the node __dirname keyword as an argument. Whe then call the recurse method, which recursively lists every file and folder in the current folder.

Summary

In this article, we learned how to do three things. First, we learned how to determine the path to the current folder. Second, we learned how to determine the names of every file in the current folder. And third, we learned how to recursively determine the names of every file and folder in the current folder. While these examples were basic, they are not uncommon scenarios. The filepath Node.js features a simple yet powerful tool set. If your Node application works with the file system, this module is one to keep on your radar.

Author

Kevin Chisholm blog.kevinchisholm.com

Share This