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 Create Quiz Comment S sayantanm19 Follow 0 Improve S sayantanm19 Follow 0 Improve Article Tags : Web Technologies Node.js Node.js-path-module Explore Introduction & Installation NodeJS Introduction3 min readNode.js Roadmap: A Complete Guide6 min readHow to Install Node.js on Linux6 min readHow to Install Node.js on Windows5 min readHow to Install NodeJS on MacOS6 min readNode.js vs Browser - Top Differences That Every Developer Should Know6 min readNodeJS REPL (READ, EVAL, PRINT, LOOP)4 min readExplain V8 engine in Node.js7 min readNode.js Web Application Architecture3 min readNodeJS Event Loop5 min readNode.js Modules , Buffer & StreamsNodeJS Modules5 min readWhat are Buffers in Node.js ?4 min readNode.js Streams4 min readNode.js Asynchronous ProgrammingAsync Await in Node.js3 min readPromises in NodeJS7 min readHow to Handle Errors in Node.js ?4 min readException Handling in Node.js3 min readNode.js NPMNodeJS NPM6 min readSteps to Create and Publish NPM packages7 min readIntroduction to NPM scripts2 min readNode.js package.json4 min readWhat is package-lock.json ?3 min readNode.js Deployments & CommunicationNode Debugging2 min readHow to Perform Testing in Node.js ?2 min readUnit Testing of Node.js Application5 min readNODE_ENV Variables and How to Use Them ?2 min readDifference Between Development and Production in Node.js3 min readBest Security Practices in Node.js4 min readDeploying Node.js Applications5 min readHow to Build a Microservices Architecture with NodeJS3 min readNode.js with WebAssembly3 min readResources & ToolsNode.js Web Server6 min readNode Exercises, Practice Questions and Solutions4 min readNode.js Projects9 min readNodeJS Interview Questions and Answers15+ min read Like