Skip to content

Instantly share code, notes, and snippets.

@woss
Created May 22, 2021 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woss/63fc730146808635662c9f611bb01282 to your computer and use it in GitHub Desktop.
Save woss/63fc730146808635662c9f611bb01282 to your computer and use it in GitHub Desktop.
ESM __dirname and __filename
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)
}
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