Understanding the path Module in Node.js

The path module is a built-in module of Node.js that provides various methods to interact with the files and directory path names on the device’s file system. The path module has numerous useful functionalities and methods that can easily access and manipulates paths present in the file system.

Since the path module is one of the core modules in Node.js, therefore, we can import the module using the following syntax:

Some Properties of the path module

  • The path module provides sep property to represent the platform-specific path-segment separator:

The path.sep returns ‘/’ on Linux and macOS, and ‘\’ on Windows.

  • The path module also provides delimiter property to represent the path delimiter:

The path.delimiter returns ‘:’ on  Linux / macOS, and ‘;’ on Windows.

  • The path module allows access to POSIX specific applications:
  • The path module also provides access to Windows-specific applications:

 Understanding methods of path module

We can use many methods in the path module for interacting with the path of a file system. Some of them are list below:

  • path.basename(path[, ext])
  • path.parse(path)
  • path.format(pathObject)
  • path.extname(path)
  • path.dirname(path)
  • path.relative(from, to)
  • path.normalize(path)
  • path.isAbsolute(path)
  • path.join([…paths])
  • path.resolve([…paths])

Let’s discuss these methods in brief.

1. path.basename(path[, ext])

The path.basename() method is used to return the trailing segment of the specified path. This method works similar as the Unix basename command. For Example:

The Output should look as follows:

2. path.parse(path)

The path.parse() method is used to return an instance whose functionalities signify important elements of the specified path. For Example:

The Output given as follows:

3. path.format(pathObject)

The path.format() method is used to return the string of a path from a specified path instance. This method is the counterpart of path.parse(). For Example:

To execute the above script file, type the following command:

The Output is given as follows:

4. path.extname(path)

The path.extname() method is used to return the extension of the specified path. It provides the extension of the path starting from the last. (period) to the End of the string in the trailing segment of the path. However, it returns an empty string if no “.” character is present in the basename or if there are no “.” characters other than the first character of the basename of the path provided. For Example:

To execute the above script file, type the following command:

The Output is given as follows:

5. path.dirname(path)

The path.dirname() method is used to return the name of the specified path’s directory. It functions same as the Unix dirname command. For Example:

The Output should look as follows:

6. path.relative(from, to)

The path.relative() method is used to discover the relative path from one path to another specified, based on the recent functioning directory. However, it will return a zero-length string, if both the input paths are the same. For Example:

The Output should look as follows:

7. path.normalize(path)

The path.normalize() method is used to normalize a specified string path. This method can also resolve the segments like ‘..’ and ‘.’ respectively. For Example:

To execute the above script file, type the following command:

The Output should look as follows:

D:\node js\ffolder\sfloder\tfolder

8. path.isAbsolute(path)

The path.isAbsolute() method is used to define whether the specified path is an absolute path or not. If the provided path is a zero-length string, it will return false as an output. For Example:

To execute the above script file, type the following command:

The Output should look as follows:

9. path.join([…paths])

Thepath.join() method takes two or more segments of a file path and combines them to form a single path with the help of the platform-specific separator as a delimiter. It then normalizes the subsequent path. For Example:

The Output should look as follows:

D:\node js\ffolder\sfolder\tfolder\good.html

10. path.resolve([…paths])

The path.resolve() method resolves the path’s sequence or the segments of the path into an absolute pathThis method prepends each subsequent path, processing from right to left until an absolute path is built. For Example:

To execute the above script file, type the following command:

The Output should look as follows:

Pin It on Pinterest

Share This