question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can’t resolve `@types` when vendoring dependencies for browser ESM imports

See original GitHub issue

Bug Report

🔎 Search Terms

  • native modules alias relative paths
  • Relative references must start with either “/”, “./”, or “…/”.

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about declaration files

⏯ Playground Link

N/A (behavior is documented)

💻 Code

Related frustrations have been posted many times before - so I’ve hesitated to post this:

https://github.com/microsoft/TypeScript/issues/16577 https://github.com/microsoft/TypeScript/issues/13422 https://github.com/microsoft/TypeScript/issues/28288 https://github.com/microsoft/TypeScript/issues/42151

My case, and workaround, are documented here: https://stackoverflow.com/questions/67725840/how-can-i-use-threejs-es6-native-modules-with-typescript/73575993#73575993

🙁 Actual behavior

  1. The Declaration File must be included with an alias, e.g. import * as THREE from 'three'
  2. es6 modules require extensions, which the alias does not allow. TypeScript does and will not support URL rewriting.
  3. As a result, I cannot use Declaration Files + TypeScript without importing additional software

🙂 Expected behavior

There could be multiple possible solutions here:

  1. Just document it better, here: https://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html If this was described as a known limitation, it would have saved me half a day.

  2. Support aliases with extensions in one of two ways: a) Allow the alias itself to be of the final expected form ./js/lib/three.module.js b) Allow some sort of a custom mapping, which I can use to tell tsc to look for an .d.ts alias three when it sees ./js/lib/three.module.js

Thanks in advance!

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
andrewbranchcommented, Sep 21, 2022

After thinking about it for about 30 seconds, I’m wondering if it needs to be more complicated than

{
  "compilerOptions": {
    "relativePaths": {
      "./node_modules/*": ["./node_modules/*", "./node_modules/@types/*"]
    }
  }
}

where unlike paths, the key side prefix doesn’t need to be an exact string match of the module specifier, but rather a prefix match after both the module specifier and the key are resolved to an absolute directory path. That is, it should match both "./node_modules/foo/index.js" and "../../../node_modules/foo/index.js" as long as both mentions of node_moduels refer to the same directory that the tsconfig mentioned.

1reaction
fatcerberuscommented, Sep 2, 2022

this isn’t syntactically valid for browsers

Small nitpick: It is syntactically valid, it’s just semantically invalid (unless the browser supports import maps, as you note). This is just a pet peeve of mine so take it with a grain of salt. 😅

An alternative solution would be to use import maps, although they’re currently Chrome only

Trying to make myself a bit more useful, I wanted to chime in on this that you can use es-module-shims to get import maps working on non-Chromium browsers. I’ve been using this in Oozaru, it works quite well. (@pehrlich2 this might be useful for you as well)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Module not found: Error: Can't resolve 'fs' in - Stack Overflow
I found a possible solution, that you have to put some configuration in one of the node_modules. But I think that is not...
Read more >
Common TypeScript module problems and how to solve them
Solution 2: Locate the module and resolve imports​​ ts . This will allow the module to be found.
Read more >
Entry Points - webpack
webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable...
Read more >
ES modules are terrible | Hacker News
It's the fact that the imports can't be variables like commonjs allows. ... Circular dependencies: ESM correctly resolves most of them, ...
Read more >
Importmaps - The Odin Project
Rails 6 tried to fix this solution by creating a wrapper Gem around ... Finally, the application entry point is imported via <script...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found