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?

3
  • Does this do what you want? Commented Aug 5, 2019 at 1:35
  • dynamic imports are useless seeing as export does not wait for them. Commented Aug 5, 2019 at 1:45
  • 1
    did not help, but I figured it out. import.meta.url Commented Aug 5, 2019 at 18:50

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.