Skip to content

Instantly share code, notes, and snippets.

@frank-dspeed
Created January 23, 2020 09:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frank-dspeed/a309b0ae4c81816c33a83731898e5b86 to your computer and use it in GitHub Desktop.
Save frank-dspeed/a309b0ae4c81816c33a83731898e5b86 to your computer and use it in GitHub Desktop.
Method to get Current File Path inside ESM
//import.meta.url
function getPath(url) {
let result = new URL(import.meta.url)
let pathname = result.pathname
let pathArray = pathname.split('/')
let basename = pathArray.pop()
let dirname = pathArray.join('/')
return { pathname, dirname,basename}
}
console.log(getPath(import.meta.url))
const currentFile = import.meta.url;
const currentDirectory = new URL(import.meta.url).pathname.split('/').slice(0,-1).join('/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment