Created
May 22, 2021 10:25
-
-
Save woss/63fc730146808635662c9f611bb01282 to your computer and use it in GitHub Desktop.
ESM __dirname and __filename
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { dirname } from 'path' | |
import esmFilename from './esmFilename' | |
/** | |
* ESM module helper for __dirname | |
* @example | |
* ``` typescript | |
* const __dirname = esmDirname(import.meta.url) | |
* ``` | |
*/ | |
export default function esmDirname(path: string): string { | |
const __filename = esmFilename(path) | |
return dirname(__filename) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { fileURLToPath } from 'url' | |
/** | |
* ESM module helper for __filename | |
* @example | |
* ``` typescript | |
* const __filename = esmFilename(import.meta.url) | |
* ``` | |
*/ | |
export default function esmFilename(path: string): string { | |
return fileURLToPath(path) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment