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 be used to extract the directory name. It throws a TypeError if this parameter is not a string value.
Return Value: It returns a string with the directory of the path.
Below programs illustrate the
path.dirname() method in Node.js:
Example 1:
Output:
/users/admin/website
.
website/post
Example 2:
Output:
File name: G:\tutorials\nodejs-path-dirname\index.js
G:\tutorials\nodejs-path-dirname
Directory name: G:\tutorials\nodejs-path-dirname
G:\tutorials
Reference: https://nodejs.org/api/path.html#path_path_dirname_path