Web Programming TutorialsLearn How to Build Path Module Utility Modules in NodeJS

Learn How to Build Path Module Utility Modules in NodeJS

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 several modules present in Node.js library. They are OS module, path modules, net module, DNS module, and domain module. All of these modules are very popular and used very commonly to build any Node based application.

Node.js Path Module
In Node.js, the Path module provides basic path utility functions to handle and transform file paths. We can import this module by using the following syntax.

// Command to import Path Module in Node.js.

var path = require("path");

Methods present in the Path Module of node.js
The following are the basic methods and their descriptions which are present in the Path Module of Node.js.

S No

Method

Description

1.

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.

2.

path.dirname (path)

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

3.

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 portion of the path or the first character of it is ‘.’, then it will return an empty string.

4.

path.format (pathObject)

This method of the Path module is used to return a path string from an object, which is opposite of path.parse (as explained above).

5.

path.isAbsolute (path)

This method of the Path module is used to determine 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.

6.

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

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

7.

path.normalize (path)

This method of the Path module is used to normalize a string path, taking care of ‘..’ and ‘.’ parts.

8.

path.parse (pathString)

This method of the Path module is used to return an object from a path string.

9.

path.relative (from, to)

This method of the Path module is used to solve the relative path from ‘from’ to ‘to’.

10.

path.resolve ([from …], to)

This method of the Path module is used to resolve to an absolute path.

Properties present in the Path Module of node.js
The following is the basic property and its description which is present in the Path Module of Node.js.

S No

Property

Description

1.

path.delimiter

It is a constant that defines the platform-specific path delimiter (i.e., ‘;’ or ‘:’).

2.

path.posix

It is a constant that provides access to aforementioned path methods, but it always interacts in a way that is posix compatible.

3.

path.sep

It is a constant that defines the platform-specific file separator (i.e., ‘\\’ or ‘/’).

4.

path.win32

It is a constant that provides access to aforementioned path methods, but it always interacts in a way that is win32 compatible.

Example on use of Path Module in Node.js Application
In the following example, we are going to demonstrate the use of the above Path module methods. Like any other Node.js application, here we are going to create a js file named PathModule.js, which has the following code.

// Command to import Path Module in Node.js.
var path = require("path");
//1. Use of path.normalize () method.
console.log ('Path normalization is : ' + path.normalize('/trail/trail1//doublehash/hash/tab/..'));
//2. Use of path.join () method.
console.log ('joint path is : ' + path.join('/trail', 'trail1', 'doublehash/hash/', 'tab', '..'));
//3. Use of path.resolve () method.
console.log('Path resolve is : ' + path.resolve('hello-nodejs.js'));
//4. Use of path.extname () method.
console.log('Path extension name is : ' + path.extname('hello-world.js'));
//5. Use of path.isAbsolute method.
console.log('Is Absolute path? : ' + path.isAbsolute('/PathModule/PathModule.js'));
//6. Use of path.dirname (p) method.
console.log('Path Directory name is : ' + path.dirname('/PathModule/PathModule.js'));
//7. Use of path.relative (from, to) method.
console.log('Relative Path is : ' + path.relative('/PathModule/PathModule.js', '/PathModule/build.gradle'));
//8. Use of path.basename () method.
console.log('Path base name is : ' + path.basename('/PathModule/PathModule.js', '.js'));
//9. Use of path.parse (pathString) () method.
console.log('Parsed path string is : ' + path.parse('/PathModule/PathModule.js'));
//10. Use of path.format (pathObject) method.
console.log('Path format is : ' + path.format(path.parse('/PathModule/PathModule.js')));

Explanation of the code
In the above code, we have imported the “path” module of the Node.js and used the following 10 methods to return the Path related values.

  • The 1st method [‘path.normalize (‘/trail/trail1//doublehash/hash/tab/..’)’] is used to return a normalized string path.
  • The 2nd method [‘path.join (‘/trail’, ‘trail1’, ‘doublehash/hash/’, ‘tab’, ‘..’)’] is used to return a normalized path after joining all the arguments.
  • The 3rd method [‘path.resolve (‘hello-nodejs.js’)’] is used to resolve to an absolute path.
  • The 4th method [‘path.extname (‘hello-world.js’)’] is used to return the file extension.
  • The 5th method [‘path.isAbsolute (‘/PathModule/PathModule.js’)’] is used to identify whether the input string is an absolute path or not and return the Boolean value as true or false”.
  • The 6th method [‘path.dirname (‘/PathModule/PathModule.js’)’] is used to return the directory name.
  • The 7th method [‘path.relative (‘/PathModule/PathModule.js’, ‘/PathModule/build.gradle’)’] is used to return the relative path from vs to.
  • The 8th method [‘path.basename (‘/PathModule/PathModule.js’, ‘.js’)’] is used to return the basename similar to the UNIX’s basename command.
  • The 9th method [‘path.parse (‘/PathModule/PathModule.js’)’] is used to return an object of the path string.
  • The 10th method [‘path.format (path.parse (‘/PathModule/PathModule.js’)’] is used to return a path string from an object.

Output
When we execute the above Node.js code, we will observe the following output on the console.

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

Source code for the Path Module of Nodejs

Conclusion: –
In this article, we discussed the various methods present in the Path module of Node.js. We have imported the Path module and use the associated methods to build a Path module Utility.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -