Skip to content

Instantly share code, notes, and snippets.

@karlrwjohnson
Created February 23, 2023 23:09
Show Gist options
  • Save karlrwjohnson/8e2a4796e85e37ffee29d21570d7ba2f to your computer and use it in GitHub Desktop.
Save karlrwjohnson/8e2a4796e85e37ffee29d21570d7ba2f to your computer and use it in GitHub Desktop.
Equivalent of __dirname in ES Modules

When converting a Node CommonJS module to an ES module, __dirname stops working.

Add this line to make it work again:

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

const __dirname = dirname(fileURLToPath(import.meta.url));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment