The path Module
The built-in `path` module provides utilities for working with file and directory paths in a way that works across operating systems (Windows uses backslashes, Unix uses forward slashes).
Using `path` instead of manually concatenating strings avoids cross-platform bugs.
Common methods
`path.join()` combines path segments safely, `path.basename()` gets the filename, and `path.extname()` gets the file extension.
Resolving paths
`path.resolve()` turns relative paths into absolute ones based on the current working directory.
const path = require('path');
console.log(path.join('folder', 'file.txt'));
console.log(path.extname('file.txt'));folder/file.txt
.txtjoin() safely combines segments; extname() extracts the extension.
console.log(path.basename('/users/ada/app.js'));
console.log(path.resolve('app.js'));app.js
/current/working/dir/app.jsbasename() returns just the file name; resolve() returns an absolute path.
Key points
- path module handles file paths cross-platform.
- path.join() safely combines path segments.
- path.basename() and path.extname() extract parts of a path.
- path.resolve() returns an absolute path.
