path.parse()
Parses a path string into an object of its significant parts.
Syntax
path.parse(path) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
path | string | Yes | The path to parse. |
Returns
object — An object with root, dir, base, ext, and name.
Examples
import { parse } from 'node:path';
console.log(parse('/users/data/report.pdf'));
Output
{
root: '/',
dir: '/users/data',
base: 'report.pdf',
ext: '.pdf',
name: 'report'
}
import { parse, format } from 'node:path';
const p = parse('/tmp/a.txt');
console.log(format({ ...p, base: 'b.txt' }));
Output
/tmp/b.txt
Notes
The inverse is `path.format()`. When both `base` and `ext`/`name`
are provided to `format`, `base` wins. Useful for renaming files
while keeping the directory.