• Node.js Video Tutorials

Node.js - path.format() Method



The path module of Node.js provides some useful utilities for working with file and directory paths. In this article, we'll go over path.format() method of the path module with appropriate examples.

The Node.js path.format() method of path module returns a string that specifies the path from an object. This method exactly opposite to the method named path.parse() of path module.

This method takes an input string and replaces placeholders with values specified in additional arguments. Placeholders can contain flags, width, alignment, or other information that control how the value is formatted. Additionally, this method supports special tokens such as %d (for integers), %f (for floats), and %j (for JSON). This makes it easy to create formatted strings in your Node.js path.applications without writing custom code.

Syntax

Following is the syntax of the Node.js path.format() method −

path.format( pathObject )

Parameters

This method accepts only one parameter which contains information about the path. The following are the properties of the pathObject.

  • dir − This property describes the directory name of the path object.

  • root − This describes the root of the path object.

  • base − This describes the base of the path object.

  • name − This property describes the file name of the path object.

  • ext − This describes the file extension of the path object.

Return value

This method returns a path string form the specified path object.

While providing the values to the pathObject, there are some combinations where one property has more priority than another −

  • The 'root' property of the pathObject will be ignored if the 'dir' property is given.

  • The 'ext' property and 'name' of the pathObject will be ignored if the 'base' property is given.

Example

If 'dir', 'root', and 'base' properties of the pathObject are specified, then the 'root' property will be ignored.

In the following example, we are trying to provide the values to 'dir', 'root', and 'base' properties of the pathObject.

const path = require('path');
var formatted = path.format({
   root: '/IGNORED',
   dir: '/Desktop/JavaScript',
   base: 'Nodefile.js'
});
console.log(formatted);

Output

After executing the above program, the 'dir' and 'base' will be returned and the 'root' property of the pathObject will be ignored.

/Desktop/JavaScript/Nodefile.js

Example

The 'root' property of the pathObject will be used, if the 'dir' property is not given.

In the following example, we are trying to give the values to the 'root', and 'base' properties of the pathObject and we haven't given the 'dir' property.

const path = require('path');
var formatted = path.format({
   root: '/Lenovo/',
   base: 'Nodefile.js'
});
console.log(formatted);

Output

After executing the above program, the 'root' will be used because there is no 'dir' property given in the pathObject.

/Lenovo/Nodefile.js

Example

If 'root' property of pathObject is given, then the platform separator will not be included.

In the following example, we are providing the 'root', 'base', and 'ext' properties of the pathObject.

const path = require('path');
var formatted = path.format({
   root: '/Lenovo/',
   base: 'Nodefile',
   ext: '.js'
});
console.log(formatted);

Output

As we can see in the output below, the 'ext' property is ignored because only 'root' property of the pathObject is given.

/Lenovo/Nodefile

Example

If the 'root' and 'dir' properties of pathObject are same then the platform separator will not be included. Then 'ext' will be ignored.

In the following example, we are providing the 'root', 'base', 'dir', and 'ext' properties of the pathObject.

const path = require('path');
var formatted = path.format({
   root: '/Lenovo/',
   dir: '/Lenovo/',
   base: 'Nodefile',
   ext: '.js'
});
console.log(formatted);

Output

As we can see in the output below, the 'ext' property is ignored because the 'root' and 'dir' properties of the pathObject are same.

/Lenovo/Nodefile

Example

The 'name' + 'ext' properties of the pathObject will be used if the 'base' is not given.

In the following example, we are trying to provide the 'root', 'name', and 'ext' properties of the pathObject, and we haven't given the 'base' property.

const path = require('path');
var formatted = path.format({
   root: '/Lenovo',
   name: '/Nodefile',
   ext: '.js'
});
console.log(formatted);

Output

After executing the above program, 'name' + 'ext' properties of the pathObject will be returned in place of 'base' property because there is no 'base' property is given.

/Lenovo/Nodefile.js
nodejs_path_module.htm
Advertisements