path.parse on a Windows-style path?import path from 'path'; const parsed = path.parse('C:\\Users\\Alice\\Documents\\file.txt'); console.log(parsed);
The path.parse method splits the path into root, dir, base, ext, and name. On Windows, the root includes the drive letter and backslash, so root is 'C:\\'. The directory is everything before the base file name.
path.format?import path from 'path'; const parsed = { root: '/', dir: '/home/user/docs', base: 'notes.txt', ext: '.txt', name: 'notes' }; console.log(path.format(parsed));
base property takes priority over name and ext when formatting.The path.format method constructs a path string from the object. If base is provided, it uses that directly as the file name, ignoring name and ext. The directory is combined with the base.
path.format output if the input object has only name and ext properties, but no dir or root?import path from 'path'; const obj = { name: 'image', ext: '.png' }; console.log(path.format(obj));
When dir and root are missing, path.format returns just the file name constructed from name and ext. So it returns 'image.png'.
path.parse in Node.js?Option A is missing the closing parenthesis and semicolon, causing a syntax error. The other options are syntactically correct.
const obj = { dir: '/var/log', base: 'app.log', name: 'app', ext: '.log' };
console.log(path.format(obj));
Why does the output use base and ignore name and ext?The path.format method uses base if it exists, ignoring name and ext. This is by design to avoid ambiguity in file naming.