Skip to content

Latest commit

 

History

History
12 lines (8 loc) · 551 Bytes

get-dirname-in-esm.md

File metadata and controls

12 lines (8 loc) · 551 Bytes

Get __dirname in ESM

When using ES modules with Node.js, __dirname (the path to the file's directory) isn't available. Here's a short and sweet replacement:

import { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const __dirname = dirname(fileURLToPath(import.meta.url));

Update: As of Node 21.2.0, __filename and __dirname are natively available as import.meta.filename and import.meta.dirname, respectively.