TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

TypeScript cannot emit valid ES modules due to file extension issue (2020)

29 pointsby new_user_finalabout 2 years ago

6 comments

haburkaabout 2 years ago
It’s disgraceful how little respect github commenters have for maintainers sometimes. They seem so entitled to having the package work the way they want it to when it’s used by millions of developers. I feel that the typescript maintainers were incredibly polite here and should have locked the issue much earlier.<p>Open source developers at big companies are often incredibly competent as well as under supported compared with those that work on products that bring in revenue. We don’t also have to emotionally attack them.
评论 #35240104 未加载
fabiancookabout 2 years ago
To tackle this, I always needed to write a little script for my TypeScript projects that were all being published as ESM:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;virtualstate&#x2F;promise&#x2F;blob&#x2F;main&#x2F;scripts&#x2F;correct-import-extensions.js">https:&#x2F;&#x2F;github.com&#x2F;virtualstate&#x2F;promise&#x2F;blob&#x2F;main&#x2F;scripts&#x2F;co...</a><p><a href="https:&#x2F;&#x2F;github.com&#x2F;virtualstate&#x2F;focus&#x2F;blob&#x2F;main&#x2F;scripts&#x2F;correct-import-extensions.js">https:&#x2F;&#x2F;github.com&#x2F;virtualstate&#x2F;focus&#x2F;blob&#x2F;main&#x2F;scripts&#x2F;corr...</a><p><a href="https:&#x2F;&#x2F;github.com&#x2F;virtualstate&#x2F;navigation&#x2F;blob&#x2F;main&#x2F;scripts&#x2F;correct-import-extensions.js">https:&#x2F;&#x2F;github.com&#x2F;virtualstate&#x2F;navigation&#x2F;blob&#x2F;main&#x2F;scripts...</a><p>This same script was replicated to near every repository I have worked on, including making its way into a few different production services where Node.js + ESM was used for deployment<p>Eventually I had turned into a utility module that could be used wider, which starts to lean into import map generation<p><a href="https:&#x2F;&#x2F;github.com&#x2F;virtualstate&#x2F;impack#usage">https:&#x2F;&#x2F;github.com&#x2F;virtualstate&#x2F;impack#usage</a><p>You can see it being used here as a function, but can be used as a cli tool too:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;virtualstate&#x2F;listen&#x2F;blob&#x2F;033bf8effcbde5c825851516bd651fdc1bd13680&#x2F;scripts&#x2F;post-build.js#L7-L14">https:&#x2F;&#x2F;github.com&#x2F;virtualstate&#x2F;listen&#x2F;blob&#x2F;033bf8effcbde5c8...</a>
gnabgibabout 2 years ago
In your tsconfig.json [0]:<p><pre><code> &quot;moduleResolution&quot;: &quot;node16&quot; </code></pre> In your TS files, even if the import is a file with the extension .ts (tsc will handle):<p><pre><code> import { test } from &#x27;.&#x2F;importMe.js&#x27;; </code></pre> [0]: <a href="https:&#x2F;&#x2F;www.typescriptlang.org&#x2F;tsconfig#moduleResolution" rel="nofollow">https:&#x2F;&#x2F;www.typescriptlang.org&#x2F;tsconfig#moduleResolution</a>
评论 #35235608 未加载
评论 #35238109 未加载
评论 #35235561 未加载
评论 #35238991 未加载
评论 #35234364 未加载
janciabout 2 years ago
This had driven me nuts recently. One of the dependencies was converted to ESM. If I want to import it I must convert my project to ESM (or use dynamic import that is hell of its own). That would not be very painful exept for the need to rewrite <i>every</i> <i>import</i> <i>statement</i> in the project. I guess TS5 with moduleResolution bundler solves that? Did not have time to try that yet.
dimitropoulosabout 2 years ago
If anyone is interested in knowing more about module resolution in TypeScript, a friend of mine did a great talk about it [0].<p>It touches on exactly this topic in the timestamp in the link.<p>[0]: <a href="https:&#x2F;&#x2F;youtu.be&#x2F;MEl2R7mEAP8?t=2360" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;MEl2R7mEAP8?t=2360</a>
crooked-vabout 2 years ago
This reminds me again of the whole mess with `paths` and the TS team refusing to do the bare minimum to make TS-&gt;JS compilation work with that feature instead of needing to rely on outside tooling.