path.dirname()
Returns the directory portion of a path.
Syntax
path.dirname(path) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
path | string | Yes | The path to evaluate. |
Returns
string — The directory name of the path.
Examples
import { dirname } from 'node:path';
console.log(dirname('/users/data/report.pdf'));
Output
/users/data
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const here = dirname(fileURLToPath(import.meta.url));
console.log(here);
Output
/home/user/project/src
Notes
Equivalent to everything before the last separator. In ESM use
`import.meta.dirname` (Node 20.11+) directly instead of computing
it from `import.meta.url`.