When I dynamically import a module, I can’t import other modules relative to the website root from the root of the site.
const fileConents = `import { test } from "/some-path.js"`;
const blob = new Blob([fileContents], {
type: "application/javascript"
});
const moduleUrl = URL.createObjectURL(blob);
try {
const importedModule = await import(moduleUrl);
} catch (err) {
console.error(err);
} finally {
URL.revokeObjectURL(moduleUrl);
}
This causes the following error:
TypeError: Failed to resolve module specifier “/some-path.js”. Invalid relative url or base scheme isn’t hierarchical.
Importing “/some-path.js” in static imports. Please advice how to fix this!
3
I have used a regex to replace /
to change it to a https://
path.
const fileConents = `import { test } from "/some-path.js"`
.replaceAll(/(^import [^"]+")//gm, `$1${window.origin}/`);