Node.js path.format() Method Last Updated : 08 Oct, 2021 Comments Improve Suggest changes Like Article Like Report 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 javascript // Import the path module const path = require('path'); // CASE 1 // If "dir", "root" and "base" are all given, // "root" is ignored. let path1 = path.format({ root: "/ignored/root/", dir: "/home/user/personal", base: "details.txt", }); console.log("Path 1:", path1); // CASE 2 // If "dir" is not specified then "root" will be used // If only "root" is provided // platform separator will not be included. // "ext" will be ignored. let path2 = path.format({ root: "/", base: "game.dat", ext: ".noextension", }); console.log("Path 2:", path2); // CASE 3 // If "base" is not specified // "name" and "ext" will be used let path3 = path.format({ root: "/images/", name: "image", ext: ".jpg", }); console.log("Path 3:", path3); Output: Path 1: /home/user/personal/details.txt Path 2: /game.dat Path 3: /images/image.jpg Example 2: On Windows javascript // Import the path module const path = require('path'); // CASE 1 // If "dir", "root" and "base" are all given, // "root" is ignored. let path1 = path.format({ root: "C:\\ignored\\root", dir: "website\\dist", base: "index.html", }); console.log("Path 1:", path1); // CASE 2 // If "dir" is not specified then "root" // will be used // If only "root" is provided platform // separator will not be included. // "ext" will be ignored. let path2 = path.format({ root: "C:\\", base: "style.css", ext: ".ignored", }); console.log("Path 2:", path2); // CASE 3 // If "base" is not specified // "name" and "ext" will be used let path3 = path.format({ root: "website\\", name: "main", ext: ".js", }); console.log("Path 3:", path3); 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 Comment More infoAdvertise with us Next Article Node.js path.isAbsolute() Method S sayantanm19 Follow Improve Article Tags : Web Technologies Node.js Node.js-path-module Similar Reads Node.js path.basename() Method The path.basename() method is used to get the filename portion of a path to the file. The trailing directory separators are ignored when using this method. Syntax: path.basename( path, extension ) Parameters: This method accepts two parameters as mentioned above and described below: path: It is the 1 min read Node.js path.dirname() Method The path.dirname() method is used to get the directory name of the given path. It ignores the respective platform's trailing directory separators. Syntax: path.dirname( path ) Parameters: This function accepts one parameter as mentioned above and described below: path: It is the file path that would 1 min read Node.js path.extname() Method The path.extname() method is used to get the extension portion of a file path. The extension string returned from the last occurrence of a period (.) in the path to the end of the path string. If there are no periods in the file path, then an empty string is returned. Syntax: path.extname( path ) Pa 2 min read Node.js path.format() Method 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 ob 3 min read Node.js path.isAbsolute() Method The path.isAbsolute() method is used to check whether the given path is an absolute path or not. An absolute path is defined as a path that contains the complete details needed to locate a file. Syntax: path.isAbsolute( path ) Parameters: This method accepts single parameter path which holds the fil 1 min read Node.js path.join() Method The path.join() method in Node.js is part of the Path module, which is used for handling and transforming file paths. The method is used to join multiple path segments together into one complete path, ensuring that the resulting path is formatted correctly for the current operating system. In this a 2 min read Node.js path.normalize() Method The path.normalize() method is used to normalize the given path. Normalization resolves the (.) and (..) segments of the path to their correct form. If the method encounters multiple path separators, it replaces all of them by a single platform-specific path separator. This method preserves all trai 1 min read Node.js path.parse() Method The path.parse() method is used to return an object whose properties represent the given path. This method returns the following properties: root (root name) dir (directory name) base (filename with extension) ext (only extension) name (only filename) The values of these properties may be different 2 min read Node.js path.relative() Method The path.relative() method is used to find the relative path from a given path to another path based on the current working directory. If both the given paths are the same, it would resolve to a zero-length string. Syntax: path.relative( from, to ) Parameters: This method accept two parameters as me 1 min read Node path.resolve() Method The path.resolve() method takes a sequence of paths or path segments and resolves them into an absolute path. It processes the paths from right to left and appends each path until an absolute path is constructed.Node path.resolve method The path.resolve() method in Node.js helps create a full, absol 3 min read Like