path.resolve() for Absolute Paths
You'll learn how to convert any relative path into a complete, absolute path using path.resolve() and how it builds paths from right to left.
What is path.resolve()?
While path.join() simply glues path segments together, path.resolve() takes path segments and constructs an absolute path — a complete address from your system's root directory. Think of it like giving someone your full mailing address instead of just "two blocks down and turn left."
How It Processes Arguments
Here's the critical difference: path.resolve() processes arguments from right to left until it constructs an absolute path:
const path = require('path');
// Starts from current working directory
console.log(path.resolve('folder', 'file.txt'));
// Output: /Users/yourname/current-directory/folder/file.txt
// Multiple segments
console.log(path.resolve('src', 'utils', 'helpers.js'));
// Output: /Users/yourname/current-directory/src/utils/helpers.js
// If any segment starts with '/', it becomes the new "root"
console.log(path.resolve('folder', '/absolute', 'file.txt'));
// Output: /absolute/file.txt (everything before '/absolute' is discarded)
Right-to-Left Resolution
The function reads arguments right to left, prepending each segment until it hits an absolute path or runs out of segments (in which case it uses process.cwd()):
// Without any absolute paths, uses current directory
path.resolve('a', 'b', 'c'); // cwd/a/b/c
// Stops at first absolute path when reading right-to-left
path.resolve('a', '/b', 'c'); // /b/c (discards 'a')
Common Use Case
const configPath = path.resolve('config', 'settings.json');
// Always gives you the absolute path, regardless of where your script runs
Key Takeaway: path.resolve() converts relative paths to absolute paths by processing arguments right to left, stopping when it encounters an absolute path or prepending the current working directory — perfect for ensuring you always reference files with complete system paths.