The path module provides utilities for working with file and directory paths.
It helps in creating, parsing, and manipulating file paths in a platform-independent manner, ensuring that your code works consistently across different operating systems.
Joining Path Segments: Concatenate multiple path segments into a single path.
Resolving Path Segments: Resolving the absolute path of a file or directory.
Parsing Path Strings: Parsing path strings into individual components (directory, file name, extension and more).
Platform Independence: Handling path separators and other platform-specific path conventions.
Building File Paths: It Constructs file paths dynamically based on variables or configuration settings.
Normalize Paths: Ensuring that paths are in a consistent and canonical format, helps prevent issues related to duplicate or inconsistent paths.
Working with File Extensions: Extracting or modifying file extensions as needed.
Resolving Relative Paths: Resolving relative paths to their absolute equivalents makes working with files and directories easier.
Handling Paths in Modules: Utilizing the `__dirname` and `__filename` global variables and path manipulation methods to ensure that module paths are resolved correctly.
Here are the Path Module Examples:
The "path.join()" method is used to join path segments and create a complete file path
const path = require('path'); const filePath = path.join('folder', 'subfolder', 'file1.txt'); console.log(filePath);
folder/subfolder/file.txt
The "path.normalize()" method is used to normalize a path by resolving `..` and `.` segments.
const path = require('path'); const normalizedPath = path.normalize('/folder/subfolder/../file.txt'); console.log(normalizedPath);
/folder/file.txt
The "path.resolve()" method is used to get the absolute path of a file or directory.
const path = require('path'); const absolutePath = path.resolve('folder', 'subfolder', 'file.txt'); console.log(absolutePath);
/path/to/current/directory/folder/subfolder/file.txt
The "path.basename()" method is used to extract the file name, and "path.extname()" is used to extract the file extension.
const path = require('path'); const filePath = '/folder/subfolder/file.txt'; const fileName = path.basename(filePath); const fileExtension = path.extname(filePath); console.log('File Name:', fileName); console.log('File Extension:', fileExtension);
File Name: file.txt File Extension: .txt
The "path.dirname()" method is used to get the directory name of a file path.
const path = require('path'); const filePath = '/folder/subfolder/file.txt'; const directoryName = path.dirname(filePath); console.log('Directory Name:', directoryName);
Directory Name: /folder/subfolder
The "path.isAbsolute()" method is used to check if a path is absolute, and "path.relative()" is used to get the relative path between two paths.
const path = require('path'); const absolutePath = '/folder/subfolder/file.txt'; const relativePath = 'folder/subfolder/file.txt'; console.log('Is Absolute Path:', path.isAbsolute(absolutePath)); console.log('Relative Path:', path.relative(absolutePath, relativePath));
Is Absolute Path: true Relative Path: folder/subfolder/file.txt