The path module provides utilities for working with file and directory paths.
It is a core Node.js module, so no installation is required.
Handles file paths across operating systems
Avoids hard-coded path separators (/ or \)
Improves portability and reliability
Simplifies path manipulation
| OS | Separator |
|---|---|
| Windows | \ |
| Linux / macOS | / |
The path module automatically manages these differences.
path.basename()Returns the last part of a path.
path.dirname()Returns the directory name of a path.
path.extname()Returns the file extension.
path.join()Joins path segments safely.
path.resolve()Resolves an absolute path.
path.normalize()Normalizes a path by removing extra slashes.
path.parse()Returns an object with path details.
Output:
path.format()Creates a path from an object.
path.isAbsolute()Checks if a path is absolute.
Relative Path
Absolute Path
__dirname
✔ Recommended for file operations
Always use path.join() instead of string concatenation
Use __dirname for reliable file access
Avoid hardcoding paths
Cross-platform compatibility
Clean and safe path handling
Easy to use
Prevents path errors
path is a core Node.js module
Used to handle and manipulate file paths
Works across different operating systems
Essential for file system operations
Take quizzes related to this topic and see where you stand!
Start Quiz Now