Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic import module is not found when created after the application started. #20945

Open
jespertheend opened this issue Oct 19, 2023 · 3 comments
Labels
working as designed this is working as intended

Comments

@jespertheend
Copy link
Contributor

To reproduce, save the following as foo.ts and run deno run -A foo.ts

await Deno.writeTextFile("bar.ts", "export const bar = 'bar'");

const {bar} = await import("./bar.ts");
console.log(bar);

This results in

error: Uncaught (in promise) TypeError: Module not found "file:///.../bar.ts".

Running it a second time works as expected.


A workaround is to make the import not statically analysable:

await import("" + "./bar.ts");

But this causes you to lose type info for bar.ts. You can cast it, but it gets a bit tedious:

const {bar} = await import("" + "./bar.ts") as typeof import("./bar.ts");

especially when using JSDoc:

const {bar} = /** @type {import("./bar.js")} */ (await import("" + "./bar.js"));
@nayeemrmn
Copy link
Collaborator

This behaviour is intended, we actually discussed it in #17697 (comment) :-)

@nayeemrmn nayeemrmn added the working as designed this is working as intended label Oct 19, 2023
@jespertheend
Copy link
Contributor Author

Ah I see 😅
I guess I was a bit too focused on not having it load on startup there. I wasn't aware this was part of the same issue.

guest271314 added a commit to guest271314/webbundle that referenced this issue Jan 1, 2024
 "" + "/path" and "/path" + "": Deno-specific workaround to avoid module not found error:
-https://www.reddit.com/r/Deno/comments/18unb03/comment/kfsszsw/
-denoland/deno#20945
-denoland/deno#17697 (comment)
@waugh
Copy link

waugh commented Feb 11, 2024

So Deno is not designed to comply with ECMAScript, which specifies the meaning and effect of a dynamic import?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
working as designed this is working as intended
Projects
None yet
Development

No branches or pull requests

3 participants