Skip to content

Instantly share code, notes, and snippets.

@guest271314
Created January 3, 2024 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guest271314/4637bb1288321256d7c14c72ebc81137 to your computer and use it in GitHub Desktop.
Save guest271314/4637bb1288321256d7c14c72ebc81137 to your computer and use it in GitHub Desktop.
Deno dynamic import("./exports") throws module not found for "exports.js" dynamically created in the script
// Deno dynamic import("./exports") throws module not found for "exports.js" dynamically created in the script
// dynamic_import_always_throws.js
// References: https://www.reddit.com/r/Deno/comments/18unb03/comment/kfsszsw/ https://github.com/denoland/deno/issues/20945
// Usage:
// deno run -A dynamic_import_always_throws.js
// bun run dynamic_import_always_throws.js
// node --experimental-default-type=module dynamic_import_always_throws.js
import { open, unlink } from "node:fs/promises";
const runtime = navigator.userAgent;
const encoder = new TextEncoder();
try {
const script = `export default 1;`;
// deno
if (runtime.includes("Deno")) {
await Deno.writeFile("exports.js", encoder.encode(script));
}
// node
if (runtime.includes("Node")) {
const dynamic = await open("./exports.js", "w");
await dynamic.write(script);
await dynamic.close();
}
// bun
if (runtime.includes("Bun")) {
await Bun.write("exports.js", encoder.encode(script));
}
const { default: module } = await import("./exports.js"); // Raw string specifier
console.log({ module });
console.log({ runtime });
} catch (e) {
console.log("Deno always throws.");
console.log({ runtime });
console.trace();
console.log(e.stack);
} finally {
console.log("Finally");
// node, bun
if (runtime.includes("Node") || runtime.includes("Bun")) {
await unlink("./exports.js");
} // deno
else if (runtime.includes("Deno")) {
await Deno.remove("./exports.js");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment