path.extname()
Returns the file extension of a path, including the leading dot.
Syntax
path.extname(path) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
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.