We use path.resolve to get the full absolute path from relative parts. It helps find the exact location of files or folders on your computer.
path.resolve for absolute paths in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Node.js
path.resolve([from ...], to)You can give one or more path parts as arguments.
The function joins them and returns the absolute path.
Examples
Node.js
const path = require('path'); const fullPath = path.resolve('folder', 'file.txt'); console.log(fullPath);
Node.js
const path = require('path'); const fullPath = path.resolve('/home/user', '../docs', 'readme.md'); console.log(fullPath);
path.resolve with no arguments returns the current working directory.Node.js
const path = require('path');
const fullPath = path.resolve();
console.log(fullPath);Sample Program
This program uses path.resolve to get the full absolute path to the file Button.js inside src/components. It prints the full path so you can see exactly where the file is on your computer.
Node.js
const path = require('path'); // Combine parts to get absolute path const absolutePath = path.resolve('src', 'components', 'Button.js'); console.log('Absolute path:', absolutePath);
Important Notes
Note: The exact output depends on your current folder when you run the code.
Tip: Use path.resolve to avoid errors with relative paths.
Summary
path.resolve creates a full absolute path from parts.
It helps your program find files correctly no matter where it runs.
Use it to combine folder and file names safely.
Practice
1. What does
path.resolve do in Node.js?easy
Solution
Step 1: Understand the purpose of path.resolve
path.resolve combines path segments into a full absolute path.Step 2: Compare with other options
Reading, deleting, or listing files are unrelated to path.resolve's function.Final Answer:
It creates an absolute path from given path segments. -> Option AQuick Check:
path.resolve = absolute path creation [OK]
Hint: Remember: resolve means make full absolute path [OK]
Common Mistakes:
- Confusing path.resolve with file reading functions
- Thinking it deletes or lists files
- Assuming it returns relative paths
2. Which of the following is the correct syntax to use
path.resolve to combine 'folder' and 'file.txt'?easy
Solution
Step 1: Check correct function call syntax
path.resolve is called with comma-separated arguments inside parentheses.Step 2: Analyze each option
path.resolve('folder', 'file.txt') uses correct syntax with separate arguments. path.resolve['folder', 'file.txt'] uses brackets incorrectly. path.resolve('folder' + 'file.txt') concatenates strings before passing to form 'folderfile.txt', which lacks a path separator and produces an incorrect path. path.resolve('folder\\file.txt') passes a single string using backslash as separator, which is incorrect on Unix systems and results in a wrong path.Final Answer:
path.resolve('folder', 'file.txt') -> Option CQuick Check:
Use commas inside parentheses for path.resolve [OK]
Hint: Use commas inside parentheses to combine paths [OK]
Common Mistakes:
- Using square brackets instead of parentheses
- Concatenating strings manually before passing
- Using backslash separators in path strings
3. Given the current directory is
/home/user, what will path.resolve('docs', 'file.txt') return?medium
Solution
Step 1: Understand path.resolve behavior with relative paths
When given relative paths, path.resolve resolves them against the current working directory.Step 2: Combine current directory with given segments
Current directory is /home/user, so resolving 'docs' and 'file.txt' results in /home/user/docs/file.txt.Final Answer:
/home/user/docs/file.txt -> Option DQuick Check:
Relative paths resolve from current directory [OK]
Hint: Relative paths resolve from current directory [OK]
Common Mistakes:
- Assuming path.resolve returns relative paths
- Confusing root directory with current directory
- Ignoring current working directory in resolution
4. What is wrong with this code snippet?
const path = require('path');
const fullPath = path.resolve['folder', 'file.txt'];
console.log(fullPath);medium
Solution
Step 1: Identify syntax error in function call
Function calls require parentheses (), not square brackets [].Step 2: Check other parts of the code
path module is imported correctly, variable name is valid, and console.log is fine for output.Final Answer:
Using square brackets instead of parentheses for function call. -> Option AQuick Check:
Function calls need parentheses () [OK]
Hint: Function calls always use parentheses, not brackets [OK]
Common Mistakes:
- Using brackets [] instead of parentheses ()
- Forgetting to import path module
- Confusing console.log with return
5. You want to get the absolute path to a file named
config.json located in a folder settings inside your project root. Your current working directory can vary. Which code correctly ensures the absolute path regardless of where the script runs?hard
Solution
Step 1: Understand __dirname vs process.cwd()
__dirname is the directory of the current script file, stable regardless of where the script is run from. process.cwd() is the current working directory, which can change.Step 2: Analyze each option
path.resolve(__dirname, 'settings', 'config.json') uses __dirname to build absolute path reliably. path.resolve('settings', 'config.json') and D depend on current working directory, which may vary. path.resolve(process.cwd(), 'settings/config.json') uses process.cwd() but with a string containing slash, which works but is less clear and can cause issues on some OS.Final Answer:
path.resolve(__dirname, 'settings', 'config.json') -> Option BQuick Check:
Use __dirname for stable absolute paths [OK]
Hint: Use __dirname to build absolute paths reliably [OK]
Common Mistakes:
- Using process.cwd() which can change unexpectedly
- Passing combined strings instead of separate parts
- Assuming relative paths always work
