Open In App

Node.js path.format() Method

Last Updated : 08 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
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




// 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




// 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



Previous Article
Next Article

Similar Reads

How to format the current date in MM/DD/YYYY HH:MM:SS format using Node?
The current date can be formatted by using Nodejs modules like Date Object or libraries like moment.js. Table of Content Using Node.js Date ObjectUsing Moment.js LibraryMethod 1: Using Node.js Date Object:The JavaScript Date Object can be used in the program by using the following command. const date = new Date();Now on this variable date, we can a
3 min read
How to format current date in MM/DD/YYYY HH:MM:SS format using JavaScript ?
Given a date and the task is to format the current date in MM/DD/YYYY HH:MM:SS format. Here are a few of the most techniques discussed with the help of JavaScript. Approach 1: Store the current date in variable.Use the string concatenation technique to insert / and : in between the month-day and day-year.Use .slice() method to format the day, month
2 min read
Difference between AI File Format and EPS File Format
AI stands for Adobe Illustrator and this file format was built to control the file size of high graphic images. This file format also works on a Postscript base. It was designed by Adobe corporation to manage vector drawings or artwork in an easier and more efficient manner. It uses the .ai file extension to save Adobe illustrator files. EPS stands
2 min read
Node.js util.format() Method
The util.format() (Added in v0.5.3) method is an inbuilt application programming interface of the util module which is like printf format string and returns a formatted string using the first argument. The formatted string contains zero or more format specifiers in which the corresponding argument value is converted and replaced. It is used as a de
5 min read
Convert relative path URL to absolute path URL using JavaScript
Given a relative URL, the task is to convert the relative URL to an absolute URL. Here, the base URL is also given. 2 approaches are discussed here, the first example has the baseURL provided by the user and the second takes it from the URL of the page. Approach 1: Get the relURL and baseURL from user.Use .split() method to split the base and relat
3 min read
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 file path that would be used to extract the filena
1 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 trailing separators. Syntax: path.normalize( path ) Pa
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 ) Parameters: This method accepts single parameter pat
2 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 for every platform. It ignores the platform's trai
2 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 file path that would be used to check if it is an abs
1 min read