Skip to content

Instantly share code, notes, and snippets.

@wmakeev
Last active October 16, 2023 06:34
Show Gist options
  • Save wmakeev/b4f511ed02b91d3382fbc1dcb8b54cf0 to your computer and use it in GitHub Desktop.
Save wmakeev/b4f511ed02b91d3382fbc1dcb8b54cf0 to your computer and use it in GitHub Desktop.
[ESM __dirname] #esm #dirname #filename #node
import * as url from "node:url";
/**
* Returns `__dirname` like value for ESM
*
* ```js
* const dirname = getDirname(import.meta.url)
* ```
* @link https://blog.logrocket.com/alternatives-dirname-node-js-es-modules/
* @param {string} fileUrl
*/
export const getDirname = (fileUrl) => url.fileURLToPath(new URL(".", fileUrl));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment