• Node.js Video Tutorials

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
nodejs_path_module.htm
Advertisements