Tag Archives: Node.js Path Module

PATH MODULE IN NODE.JS

Path Module in Node.js

In this article, we are going to discuss building a Path module utility by using one of the module present in the Node.js modules library. There are numerous modules present in Node.js library such as OS module, path modules, net module, DNS module, and domain module. All of these modules are quite popular and used very commonly to build any Node based application.

Node.js Path Module:

1 // Command to import Path Module in Node.js.
2
3 var path = require(“path”);

Methods present in the Path Module of Node.js:

Below we have listed the basic methods and their descriptions which are present in the Path Module of Node.js.

path.basename (path, ext):

This method of the Path module is used to return the last portion of a path. It is similar to the Unix base name command.

path.dirname (path):

This method of the Path module is used to return the directory name of a path. It is akin to the Unix dirname command.

path.extname (path):

This method of the Path module is used to return the extension of the path, from the last ‘.’ to end of string in the last portion of the path. In the case, when there is no ‘.’ in the last part of the path or the first character of it is ‘.’, then it will return an empty string.

path.isAbsolute (path):

This method of the Path module is used to define whether the path is an absolute path or not. Such an absolute path will always resolve to the same location, regardless of the working directory.

path.join ([path1][, path2][, …]):

This method of the Path module is used to join all the arguments together and regularize the resultant path.

path.win32:

It is a constant that provides access to above-mentioned path methods, but it always interrelates in a way that is win32 compatible.

Illustration on use of Path Module in Node.js Application:

1 // Command to import Path Module in Node.js.
2 var path = require(“path”);
3 //1. Use of path.normalize () method.
4 console.log (‘Path normalization is : ‘ + path.normalize(‘/trail/trail1//doublehash/hash/tab/..’));
5 //2. Use of path.join () method.
6 console.log (‘joint path is : ‘ + path.join(‘/trail’, ‘trail1′, ‘doublehash/hash/’, ‘tab’, ‘..’));
7 //3. Use of path.resolve () method.
8 console.log(‘Path resolve is : ‘ + path.resolve(‘hello-nodejs.js’));
9 //4. Use of path.extname () method.
10 console.log(‘Path extension name is : ‘ + path.extname(‘hello-world.js’));
11 //5. Use of path.isAbsolute method.
12 console.log(‘Is Absolute path? : ‘ + path.isAbsolute(‘/PathModule/PathModule.js’));
13 //6. Use of path.dirname (p) method.
14 console.log(‘Path Directory name is : ‘ + path.dirname(‘/PathModule/PathModule.js’));
15 //7. Use of path.relative (from, to) method.
16 console.log(‘Relative Path is : ‘ + path.relative(‘/PathModule/PathModule.js’, ‘/PathModule/build.gradle’));
17 //8. Use of path.basename () method.
18 console.log(‘Path base name is : ‘ + path.basename(‘/PathModule/PathModule.js’, ‘.js’));
19 //9. Use of path.parse (pathString) () method.
20 console.log(‘Parsed path string is : ‘ + path.parse(‘/PathModule/PathModule.js’));
21 //10. Use of path.format (pathObject) method.
22 console.log(‘Path format is : ‘ + path.format(path.parse(‘/PathModule/PathModule.js’)));

Output:

When the user executes the above Node.js code, the user can observe the following output on the console.

1 joint path is : \trail\trail1\doublehash\hash
2 Path resolve is : C:\odesk\Abhishek Thakur\NodeJS\PathModule\hello-nodejs.js
3 Path extension name is : .js
4 Is Absolute path? : true
5 Path Directory name is : /PathModule
6 Relative Path is : ..\build.gradle
7 Path base name is : PathModule
8 Parsed path string is : [object Object]
9 Path format is : /PathModule\PathModule.js