Loading

Quipoin Menu

Learn • Practice • Grow

node-js / Path Module
interview

Q1. What is the path module in Node.js?
The path module provides utilities for working with file and directory paths. It's a core module, so you don't need to install it. You include it with:
const path = require('path')
. It handles path operations in a platform-independent way, automatically using the correct path separators (/ on Unix, on Windows).

Q2. What are common methods of the path module?
Common methods include: path.join() (joins path segments), path.resolve() (resolves sequence to absolute path), path.basename() (gets filename), path.dirname() (gets directory), path.extname() (gets file extension), path.parse() (returns object with path parts), and path.format() (creates path from object).

Q3. What's the difference between path.join() and path.resolve()?
path.join() simply joins path segments using the platform-specific separator and normalizes the result. path.resolve() resolves a sequence of paths into an absolute path, processing from right to left until an absolute path is found. If no absolute path is found, it uses the current working directory. resolve() can handle .. and . segments.

Q4. How do you get file extension with path module?
Use path.extname() method. It returns the extension of the file path including the dot. Example: path.extname('index.html') returns '.html'. For files with multiple dots, it returns the last extension: path.extname('archive.tar.gz') returns '.gz'. If there's no extension, it returns empty string.

Q5. How does path module handle different operating systems?
The path module automatically adapts to the operating system's path conventions. On Windows, it uses backslashes; on POSIX (Linux/Mac), it uses forward slashes. You can also access path.posix and path.win32 for platform-specific operations. This makes your code portable across different environments.