Deno: Resolve dynamic imports of bare module specifiers at runtime

Created on 3 Jul 2020  路  10Comments  路  Source: denoland/deno

Seems that bare module specifiers aren't resolved at runtime.

Example

try {
  await import("bare_module_specifier");
} catch (err) {
  console.log(err);
}

Results in the following compile time error

error: TS2307 [ERROR]: Cannot find module 'bare_module_specifier' or its corresponding type declarations.
 await import("bare_module_specifier");

This should be resolved at runtime, that's kind of the point of dynamic imports with top level await.

Most helpful comment

I see. The title is confusing, along with the code example.

I suspect we should just swallow compile time dynamic import errors.

All 10 comments

That isn't a runtime error, that is a TypeScript error. You can't catch TypeScript errors.

That isn't a runtime error, that is a TypeScript error. You can't catch TypeScript errors.

I know, like I said: "Seems that bare module specifiers aren't resolved at runtime." I'm saying that this should probably be a runtime check, at-least in the case dynamic imports.

@caspervonb That's not the case, you aren't even getting to the runtime. Typescript cant compile your code...

That's not the case, you aren't even getting to the runtime. Typescript cant compile your code...

I know, again, I'm saying we should change this into a runtime check.

I see. The title is confusing, along with the code example.

I suspect we should just swallow compile time dynamic import errors.

@kitsonk @caspervonb isn't it one of TS features that it analyses statically resolvable dynamic imports? I think following example would also work:

try {
  const s = "bare_module_specifier";
  await import(s);
} catch (err) {
  console.log(err);
}

If I'm not mistaken we use that feature in bundling (ref https://github.com/denoland/deno/issues/6156)

Yeah, after I commented I had that in the back of my mind. How would this suddenly ever become resolvable at runtime? It would always be an error, unless there is an import map, but then why wouldn't you use the import map at design time?

Dynamic imports in general dont work in Javascript-only Deno, unless you use the variable workaround mentioned above or specify --no-check, which probably isnt very intuitive when running a plain JS file. Would be great to see this changed.

@kitsonk is this issue actionable after the compiler refactors?

Worth testing that it doesn't cause problems. It shouldn't now, but we don't specifically test for it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

watilde picture watilde  路  3Comments

ry picture ry  路  3Comments

xueqingxiao picture xueqingxiao  路  3Comments

ry picture ry  路  3Comments

benjamingr picture benjamingr  路  3Comments