path.basename()
Returns the last portion of a path, optionally stripping an extension.
Syntax
path.basename(path[, suffix]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
path | string | Yes | The path to evaluate. |
suffix | string | No | An extension to remove from the result if present. |
Returns
string — The trailing path segment.
Examples
import { basename } from 'node:path';
console.log(basename('/users/data/report.pdf'));
Output
report.pdf
import { basename } from 'node:path';
console.log(basename('/users/data/report.pdf', '.pdf'));
Output
report
Notes
Trailing separators are ignored. The suffix is removed only if it
matches exactly at the end; it is not treated as a glob or regex.