path.parse()

Parses a path string into an object of its significant parts.

Since Node 0.11 Spec ↗

Syntax

path.parse(path)

Parameters

NameTypeRequiredDescription
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.

See also