The
path.format() method is used to return a path string from the given path object. The method has some rules where one path property gets more priority over another:
- The "root" parameter of the path object is ignored if the "dir" parameter is provided.
- The "ext" and "name" parameter of the path object are ignored if the "base" parameter is provided.
Syntax:
path.format( pathObject )
Parameters: This function accepts single parameter
pathObject that contains the details of the path. It has the following parameters:
- dir: It specifies the directory name of the path object.
- root: It specifies the root of the path object.
- base: It specifies the base of the path object.
- name: It specifies the file name of the path object.
- ext: It specifies the file extension of the path object.
Return Value: It returns a path string from the provided path object.
Below programs illustrate the path.format() method in Node.js:
Example 1: On POSIX
Output:
Path 1: /home/user/personal/details.txt
Path 2: /game.dat
Path 3: /images/image.jpg
Example 2: On Windows
Output:
Path 1: website\dist\index.html
Path 2: C:\style.css
Path 3: website\main.js
Reference: https://nodejs.org/api/path.html#path_path_format_pathobject