1

If some JS code has this

import("path/to/file.js")

and then file.js has this

export default async function() {
    // I want to get "path/to" here
    return {};
}

How can I get the directory of where file.js is?

omega
  • 40,311
  • 81
  • 251
  • 474

1 Answers1

1
export default async function() {
    var current_file = import.meta.url;
    var dir_path = import.meta.url.substring(0, import.meta.url.lastIndexOf("/"));
    return {};
}

See compatibility here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import.meta

omega
  • 40,311
  • 81
  • 251
  • 474