Path

path.extname()

Extracts the final extension including its leading dot. Tested on Node.js 22.16.0. An extension is not proof of file content or MIME type.

Formal Syntax & Structure

nodejs syntaxnodejs
path.extname(path)

Parameters & Attributes

NameTypeDescription
pathstringPath text.

Return Value

string

Extension or empty string.

Working Example

Example Snippetnodejs
import path from 'node:path';console.log(JSON.stringify([path.posix.extname('archive.tar.gz'),path.posix.extname('.gitignore')]));
Node.js Reference