Node.js path.parse() Method Last Updated : 13 Oct, 2021 Comments Improve Suggest changes Like Article Like Report 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 trailing directory separators during parsing. Syntax: path.parse( path ) Parameters: This method accepts single parameter path which holds the file path that would be parsed by the method. It throws a TypeError if this parameter is not a string value. Return Value: This method returns an object with the details of the path. Below examples illustrate the path.parse() method in node.js: Example 1: On POSIX javascript // Node.js program to demonstrate the // path.parse() method // Import the path module const path = require('path'); path1 = path.parse("/users/admin/website/index.html"); console.log(path1); path2 = path.parse("website/readme.md"); console.log(path2); Output: { root: '/', dir: '/users/admin/website', base: 'index.html', ext: '.html', name: 'index' } { root: '', dir: 'website', base: 'readme.md', ext: '.md', name: 'readme' } Example 2: On Windows javascript // Node.js program to demonstrate the // path.parse() method // Import the path module const path = require('path'); path1 = path.parse("C:\\users\\admin\\website\\index.html"); console.log(path1); path2 = path.parse("website\\style.css"); console.log(path2); Output: { root: 'C:\\', dir: 'C:\\users\\admin\\website', base: 'index.html', ext: '.html', name: 'index' } { root: '', dir: 'website', base: 'style.css', ext: '.css', name: 'style' } Reference: https://nodejs.org/api/path.html#path_path_parse_path Comment More infoAdvertise with us Next Article Node.js path.relative() Method sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies Node.js Node.js-Methods 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