tmcw
tmcw10mo ago

Is there any way to get Deno to not statically analyze import()?

I understand that statically analyzing and fetching dynamic import() calls is a good thing for performance, but it's making my usecase impossible - I want to import user code, which may crash, and to handle the error if it, say, throws an error on import. I can do this with browser import(). I can do it with Node. I can't do it with Deno: you basically can't use import statements like you'd expect to if you read the MDN page, which seems like a major move away from this being web-standardsy and also makes dynamic import way less useful. Is there a way to actually dynamically load code, at runtime, that I'm missing?
5 Replies
Deno AI Helper
Deno AI Helper10mo ago
Based on the documentation, Deno does statically analyze dynamic imports when you're using the deno compile command. This means that if your dynamic import statements are statically analyzable (if you used string literals inside import()), the specifiers will be automatically included in the produced binaries. However, the documentation also mentions that non-statically analyzable dynamic imports won't be included by default. For example, if the import specifier is determined at runtime, Deno won't be able to include it in the compiled binary. To include non-statically analyzable dynamic imports, you can specify an --include <path> flag when running deno compile. This will ensure that the specified module is included in the compiled binary, even if it's not statically analyzable. As for handling errors during the import process, the documentation doesn't provide a clear answer. However, you might be able to use a try-catch block to handle any errors that occur when importing a module. If the imported module throws an error on import, the catch block should be able to handle it. Please note that this information is based on the documentation's opinion and your actual experience may vary. If you're having trouble with this, I would recommend asking for help in the Deno community or filing an issue on the Deno GitHub repository. 🦕
bartlomieju
bartlomieju10mo ago
Could you elaborate on what is not working? Besides fetching the code Deno doesn't execute it until you actually call import() If you really need to stop it eagerly fetching the source file something like import("" + "...") should do the job I would appreciate a bug report for this
tmcw
tmcw10mo ago
there's a bug report i've commented on at https://github.com/denoland/deno/issues/18557 - if you have a dynamic import that points to a module that has a git+ dependency, your import() method is permanently broken
GitHub
Support npm packages with git dependencies · Issue #18557 · denolan...
On Arch Linux using the Arch Communty Repository deno, after upgrading to canary (which seems to be already done for the package) it does not work when i try to import npm:activitypub-express@4.2.2...
tmcw
tmcw10mo ago
that's been a fairly tough one to work with - with val.town at least there are certain dependencies that just break the evaluation server until it restarts, though evaluations are happening in a Worker
tmcw
tmcw10mo ago
Another issue for another part of this - doesn't seem like "" + works for npm: packages - https://github.com/denoland/deno/issues/20479
GitHub
Can't dynamically import an npm module with static analysis off · I...
With the following code: await import("" + "npm:got"); I get: ➜ git:(v3) ✗ deno run foo.mjs error: Uncaught (in promise) TypeError: Loading unprepared module: npm:got, imported ...