path.extname()

Returns the file extension of a path, including the leading dot.

Since Node 0.x Spec ↗

Syntax

path.extname(path)

Parameters

NameTypeRequiredDescription
path string Yes The path to evaluate.

Returns

string — The extension including the dot, or an empty string.

Examples

import { extname } from 'node:path';

console.log(extname('archive.tar.gz'));
Output
.gz
import { extname } from 'node:path';

console.log(extname('README'));
console.log(extname('.gitignore'));
Output
<empty>
<empty>

Notes

Only the final extension is returned (`.gz`, not `.tar.gz`). A leading-dot dotfile with no other dot returns an empty string.

See also