Core Modules
Node.js path Module
The path module builds and inspects file paths correctly on every operating system, handling the difference between forward and backslashes so your code works on Windows, macOS and Linux alike.
What is path Module in Node.js?
The path module builds and inspects file paths correctly on every operating system, handling the difference between forward and backslashes so your code works on Windows, macOS and Linux alike.
path Module example
JavaScript
import path from 'node:path';
path.join('uploads', 'resumes', 'cv.pdf'); // uploads/resumes/cv.pdf
path.resolve('uploads', 'cv.pdf'); // absolute from cwd
path.extname('resume.pdf'); // .pdf
path.basename('/a/b/resume.pdf'); // resume.pdf
path.basename('/a/b/resume.pdf', '.pdf'); // resume
path.dirname('/a/b/resume.pdf'); // /a/b
path.parse('/a/b/resume.pdf'); // { root, dir, base, ext, name }Key points to remember
- join concatenates segments; resolve produces an absolute path.
- Never build paths with string concatenation and slashes.
- path.normalize collapses .. and . segments.
Common mistakes with path Module
- Joining user input into a path without validation — a "../" segment escapes the intended directory.
- Hardcoding forward slashes and breaking on Windows.
Node.js path Module— Interview Questions & FAQs
What is the difference between path.join and path.resolve?+
join simply concatenates segments with the correct separator. resolve processes segments right to left until it forms an absolute path, treating a leading slash as the root.
