
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
Node.js - path.dirname() Method
The Node.js path.dirname() method of path module is a helper method to get the parent directory for a specified path. This method can be useful when you need to determine which folder an application or script is running from, or if you want to find out where in your file system a certain file resides.
Whereas, on LINUX based systems, we can get the directory name portion of the path by using dirname command. Trailing directory separators are ignored in this method.
Syntax
Following is the syntax of the Node.js path.dirname() method of path module −
path.dirname( path )
Parameters
path − This parameter holds the file path that would be used to extract the directory name of that particular file. A TypeError is thrown as path is not a string.
Return value
This method returns a string which specifies the directory name of the specified file path. This helps parse a path to determine where a file is located relative to another file or folder.
Example
If we pass the path parameter to the method, it will return the directory name portion of the specified path.
In the following example, we are trying to get the directory name of the specified file path (Nodefile.js) by using the Node.js path.dirname() method of the os module.
const path = require('path'); const path1 = path.dirname("C:/Users/Lenovo/Desktop/JavaScript/Nodefile.js"); console.log("The Directory name of the file path (Nodefile.js) is: " + path1);
Output
After executing the above program, the ,path.dirname() returns the directory name portion of the given file path.
The Directory name of the file path (Nodefile.js) is: C:/Users/Lenovo/Desktop/JavaScript
Example
If we pass a value which is not a string type to the path parameter, the method will throw a TypeError.
In the following example, we are passing integer instead of string to the path parameter of the method.
const path = require('path'); const path1 = path.dirname(6576543); console.log("The Directory name of the path (Nodefile.js) is: " + path1);
TypeError
If we compile and run the above program, the path.dirname() method throws an TypeError as the path parameter is not a string value.
For Output Code pre classpath.js:39 throw new ERR_INVALID_ARG_TYPE('path', 'string', path); ^ TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type number at assertPath (path.js:39:11) at Object.dirname (path.js:1270:5) at Object.<anonymous> (/home/cg/root/63a028ab0650d/main.js:3:20) at Module._compile (internal/modules/cjs/loader.js:702:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10) at Module.load (internal/modules/cjs/loader.js:612:32) at tryModuleLoad (internal/modules/cjs/loader.js:551:12) at Function.Module._load (internal/modules/cjs/loader.js:543:3) at Function.Module.runMain (internal/modules/cjs/loader.js:744:10) at startup (internal/bootstrap/node.js:238:19)
Example
Following is another way to get the directory name portion of the specified-file path.
const path = require('path'); console.log("The file path of (Nodefile.js): " + __filename); const path1 = path.dirname(__filename); console.log("The Directory name portion of the file is: " + path1);
Output
If we execute the code in online compiler, it will display the results according to POSIX operating system.
Following is the output of the above program −
The file path of (Nodefile.js): /home/cg/root/63a028ab0650d/main.js The Directory name portion of the file is: /home/cg/root/63a028ab0650d
When we execute the above program on WINDOWS operating system, the "__filename" will get the path of the current file and the path.dirname() method will return the directory name portion of the current file path.
The file path of (Nodefile.js): C:\Users\Lenovo\Desktop\JavaScript\nodefile.js The Directory name portion of the file is: C:\Users\Lenovo\Desktop\JavaScript