Path

path.basename()

Extracts the final path component, optionally dropping an exact suffix. Tested on Node.js 22.16.0. Use win32 explicitly for Windows-formatted inputs; this is lexical, not a filesystem lookup.

Formal Syntax & Structure

nodejs syntaxnodejs
path.basename(path[, suffix])

Parameters & Attributes

NameTypeDescription
pathstringPath text.
suffixstringSuffix to remove if it matches.

Return Value

string

Final component.

Working Example

Example Snippetnodejs
import path from 'node:path';console.log(path.posix.basename('/catalog/report.csv', '.csv'));
Node.js Reference