Node.js Path Module (original) (raw)
Last Updated : 25 Apr, 2026
The path module provides utilities to handle and manipulate file and directory paths in a platform-independent way.
- Ensures consistent behavior across different operating systems
- Supports operations like joining, resolving, and parsing paths
- Helps avoid errors in manual path handling
Installation Step (Optional)
Installation is an optional step as the path module is a built-in module in Node.js. Install the assert module using the following command:
npm install path
Importing the Module
To start using the path module in your Node.js application, import it as follows:
const path = require('path');
Path Methods
The path module provides methods to create, analyze, and manipulate file and directory paths in a platform-independent manner.
| Method | Description |
|---|---|
| path.join() | Joins multiple path segments into a single path. |
| path.resolve() | Resolves a sequence of paths into an absolute path. |
| path.normalize() | Normalizes a path by resolving . and .. segments. |
| path.isAbsolute() | Returns true if the path is an absolute. |
| path.basename() | Returns the last portion of a path (file name). |
| path.dirname() | Returns the directory name of a path. |
| path.extname() | Returns the file extension of a path. |
| path.parse() | Parses a path into an object (root, dir, base, etc.). |
| path.format() | Constructs a path string from a parsed path object. |
| path.relative() | Computes the relative path between two paths. |
| path.sep | Platform-specific path separator (/ on POSIX, \ on Windows). |
| path.delimiter | Platform-specific delimiter (: on POSIX, ; on Windows). |
Features of Path Module
The Path module simplifies file path handling by providing consistent, cross-platform utilities.
- **Cross-Platform Path Handling : Allows platform-independent file path handling.
- **Path Resolution and Normalization : Resolves and normalizes file paths.
- **Path Parsing and Formatting : Extracts and formats path components.
- **Convenient Path Operations : Simplifies path joining and resolving.
**Example: Uses path.extname() to extract a file extension, useful for identifying or filtering file types.
JavaScript ``
console.clear(); const path = require('path');
// Define a file path const filePath = '/users/gfg/documents/report.pdf';
// Extract the file extension const fileExtension = path.extname(filePath);
console.log(File Extension: ${fileExtension});
``
**Output:
File Extension: .pdf
**Example: Uses path.resolve() to convert a sequence of path segments into an absolute path.
JavaScript `
console.clear(); const path = require('path'); // Resolving an absolute path from segments const absolutePath = path.resolve('users', 'gfg', 'documents'); console.log(absolutePath);
`
**Output
/your/current/directory/users/gfg/documents
Benefits of Path Module
- **Simplified File Operations: By offering a set of easy-to-use methods, the path module simplifies complex file path operations, reducing the potential for errors.
- **Built-In and Ready to Use: As a core Node.js module, path requires no external dependencies, ensuring that you can start using it immediately without additional setup.
- **Improved Code Readability: Using path methods makes your code cleaner and easier to understand, as it reduces the need for manual string manipulation.
- **Enhanced Reliability: The path module handles edge cases and anomalies in file paths, leading to more robust and error-free code.
Also Check:
- Node.js path.basename() Method
- Node.js path.dirname() Method
- Node.js path.extname() Method
- Node.js path.format() Method
- Node.js path.isAbsolute() Method
- Node.js path.join() Method
- Node.js path.normalize() Method
- Node.js path.parse() Method
- Node.js path.relative() Method
- Node path.resolve() Method
- Node.js path.toNamespacedPath() Method
- Node.js path.delimiter Property
- Path Module Complete Reference