0
0
Node.jsframework~5 mins

path.parse and path.format in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does path.parse() do in Node.js?

path.parse() takes a file path string and breaks it into an object with parts like root, dir, base, ext, and name. It helps you understand or use each part separately.

Click to reveal answer
beginner
What are the main properties returned by path.parse()?

The main properties are:

  • root: The root of the path (like '/' or 'C:\\')
  • dir: The directory path without the file
  • base: The file name with extension
  • ext: The file extension (like '.txt')
  • name: The file name without extension
Click to reveal answer
beginner
What does path.format() do in Node.js?

path.format() takes an object with path parts (like dir, base, name, ext) and combines them into a full path string. It's like putting puzzle pieces back together.

Click to reveal answer
intermediate
How do path.parse() and path.format() work together?

You can use path.parse() to split a path into parts, change or inspect those parts, then use path.format() to build a new path string from the parts. It's like breaking a sentence into words, changing a word, then joining it back.

Click to reveal answer
beginner
If you have { dir: '/home/user', base: 'file.txt' }, what will path.format() return?

It will return the string '/home/user/file.txt'. It joins the directory and the base file name into a full path.

Click to reveal answer
Which method breaks a file path into parts like root, dir, base, ext, and name?
Apath.parse()
Bpath.format()
Cpath.join()
Dpath.resolve()
Which method combines parts of a path object into a full path string?
Apath.parse()
Bpath.format()
Cpath.basename()
Dpath.dirname()
What property in the object returned by path.parse() contains the file extension?
Aname
Bbase
Cext
Ddir
If you want to change the file name but keep the directory, which method helps you split the path first?
Apath.format()
Bpath.extname()
Cpath.join()
Dpath.parse()
Given { root: '/', dir: '/home/user', base: 'file.txt', ext: '.txt', name: 'file' }, what will path.format() return?
A/home/user/file.txt
B/file.txt
C/home/user/file
Dfile.txt
Explain how you can use path.parse() and path.format() together to rename a file in a path.
Think about breaking the path, changing the file name, then joining it back.
You got /3 concepts.
    Describe the difference between the base and name properties in the object returned by path.parse().
    One includes the extension, the other does not.
    You got /2 concepts.