Node.js Path Module

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:

const path = require(‘path’);

Some Properties of the path module

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

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

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

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

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

 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:

// Importing the 'path' module
const path = require ('path');
// This method returns basename
const mybasename = path.basename('/ffolder/sfolder/tfolder/good.html');
console.log("This is the base-name: " + mybasename);
To execute the above script file, type the following command:
$ node path.js

The Output should look as follows:

This is the base-name: good.html

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:

// Importing the 'path' module
const path = require ('path');
// This method returns objects whose properties signify important elements
const myparse = path.parse('D:/node js/ffolder/sfolder/welcome.html');
console.log(myparse);
To execute the above script file, type the following command:
$ node path.js

The Output given as follows:

{
  root: 'D:/',
  dir: 'D:/node js/ffolder/sfolder',
  base: 'welcome.html',
  ext: '.html',
  name: 'welcome'
}

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:

// Importing the 'path' module
const path = require ('path');
// This method returns a path string
const myformat = path.format({
    dir: 'ffolder\\sfolder\\tfolder',
    base: 'good.html'
});
console.log(myformat);

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

$ node path.js

The Output is given as follows:

 ffolder\sfolder\tfolder\good.html 

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:

// Importing the 'path' module
const path = require ('path');
// This method returns the extension of a path
console.log(path.extname('ffolder\\welcome.html'));
console.log(path.extname('hello.js'));
console.log(path.extname('my.world.com'));
console.log(path.extname('.hello'));

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

$ node path.js

The Output is given as follows:

.html
.js 
.com

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:

// Importing the 'path' module
const path = require ('path');
// This method returns the directory name of a path
console.log(path.dirname('ffolder/sfolder/tfolder/good.html'));
To execute the above script file, type the following command:
$ node path.js

The Output should look as follows:

ffolder/sfolder/tfolder

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:

// Importing the 'path' module
const path = require ('path');
// This method returns the relative path
console.log(path.relative('ffolder\\sfolder\\tfolder','\\sfolder\\tfolder'));
To execute the above script file, type the following command:
$ node path.js

The Output should look as follows:

..\..\..\..\sfolder\tfolder

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:

// Importing the 'path' module
const path = require ('path');
// This method normalizes a specified path
const normal = path.normalize('D:\\node js/ffolder\\sfloder/tfolder');
console.log(normal);

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

$ node path.js

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:

// Importing the 'path' module
const path = require ('path');
// This method normalizes a specified path
console.log(path.isAbsolute('D:\\node js\\'));
console.log(path.isAbsolute('/node js/'));
console.log(path.isAbsolute('/node js'));
console.log(path.isAbsolute('node js/'));
console.log(path.isAbsolute('.'));

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

$ node path.js

The Output should look as follows:

true
true
true
false
false

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:

// Importing the 'path' module
const path = require ('path');
// This method is used to join the paths
console.log(path.join('D:\\node js', 'ffolder\\sfolder', 'tfolder\\good.html'));
To execute the above script file, type the following command:
$ node path.js

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 path. This method prepends each subsequent path, processing from right to left until an absolute path is built. For Example:

// Importing the 'path' module
const path = require ('path');
// This method is used to join the paths
console.log(path.resolve('node js', 'ffolder\\sfolder', 'welcome.html'));

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

$ node path.js

The Output should look as follows:

D:\NODE JS\node js\ffolder\sfolder\welcome.html