Dev Helpers

Node.js / POSIX paths

Extract a filename

Dev Helpers · August 21, 2025

The basename is the last named part of a path. Removing an extension is a separate operation with case-sensitive edge cases.

Worked example

import { posix as path } from "node:path";
console.log(path.basename("/tmp/report.csv"));

Result

report.csv

Implementation notes

A user-facing filename might contain several suffixes. Decide whether the display should preserve the complete name or remove a recognized extension.

Try it yourself

Compare a trailing separator, a dotfile and an uppercase extension.

Open the Node.js example →

Editorial date August 21, 2025 · Published December 6, 2026

Related articles

Join path segments

Joining path segments handles separators across supported platforms. Decide whether the result is intended for a local filesystem or a web URL.

Normalize dot segments

Normalizing dot segments simplifies the spelling of a path. It does not establish that the target exists or resolve symbolic links.

Read a file extension

The last extension alone cannot identify every compound file format. Treat extension parsing as a filename convention rather than content detection.