简体   繁体   中英

How to get script url from file being imported in es6 modules javascript?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM