Closed cyrilletuzi closed 6 years ago
I have stumbled on this problem too! I can't believe a simple solution hasn't been (and probably will never be) officially provided : 0
Just started using TypeScript, and I'm extremely confused by this. I write import {foo} from './dep'
in my TypeScript - fine, this is TypeScript semantics. But then tsc
compiled this to the incorrect JavaScript import { foo } from './dep'
. "That's weird", I thought, "What is my JavaScript interpreter supposed to do with this? OK, but there must be a compiler setting for it."
But now I'm on a GitHub issue from 3 years ago saying it's intentional, with no rationale, and there's no recommended way to fix it? WTF? It's just appending a file extension! The very same file extension that TypeScript already appended when it generated dep.js
!
The TypeScript homepage states
TypeScript code is transformed into JavaScript code via the TypeScript compiler or Babel. This JavaScript is clean, simple code which runs anywhere JavaScript runs: In a browser, on Node.JS or in your apps.
No it doesn't! The browser does not run this code. Node.JS does not run this code. Find me an app that runs this code.
If you use Visual Studio Code you can configure it to automatically add the .js-ending when auto-importing modules. Go to settings, then type "module specifier" in the search field. You find option "typescript > Preferences: Import Module Specifier Ending" which you can set to ".js". This mitigates the problem a little bit...
Wow this is crazy. I wouldn't have thought an issue affecting me right now that seems to have a cut-and-dry solution to it from 3 years ago has not been resolved. Is really that few people are being affected by native import that this is not worth fixing?
Just ran into this issue and it surprises me that it hasn't been fixed. Workarounds are called workarounds for a reason - even if they work, they have their own problems.
Just ran into this issue and it surprises me that it hasn't been fixed. Workarounds are called workarounds for a reason - even if they work, they have their own problems.
Unfortunately I've found out that this bug is now considered "working as intended" and affirmatively closed by @RyanCavanaugh in a recent issue in January 2021. According to him there won't be a fix even behind a flag, so.
I'm not picking on Ryan though as I imagine this is not his decision alone, but so far I haven't seen a satisfying rationale, across 3 issues and hundreds of comments, as to why this fix is not worth doing. From Ryan it seems that complexity is a problem, and that you should write import specifiers how they're supposed to be during runtime. But I think his reasons pale in front of this argument. I would very much appreciate a response to the linked comment from the TS team.
Please consider reopening this and implementing it.
It seems that proper way fix will not be available soon, so I wrote a small js-script as an alternative to sed
-based patcher mentioned above.
To save someone else's time:
Given how the ecosystem has changed since 2017. To try summarize the TS position here in mid 2021:
TypeScript won't be changing your import specifiers for you (e.g. import {x} from "./x/y/z.ts"
to import {x} from "./x/y/z.js"
) it breaks one of the founding guidelines of only erasing types (and TS would not be where it is today if it broke its own design rules) - you can write this in ESM code today and it will work. I do it.
An import like import { Component } from '@angular/core'
only works today with node-style resolution strategies, but the import map spec for JS will solve that problem, and once it's settled and standardized TypeScript will support that too.
Re: original point about resolving TS in modules in html files- I've been doing some work on that in https://github.com/microsoft/vscode/pull/121517
Instead of waiting X years for caniuse import maps to be implemented by all browsers, typescript could implement this itself.
For example, typescript already has something similar for lookup (paths), but it doesn't transform output js import paths:
"paths": {
"jquery": ["node_modules/jquery/dist/jquery"]
}
Adding an import map feature, for example:
"importMap": {
"jquery": ["/node_modules/jquery/dist/jquery.js"]
}
would allow to transform import $ from "jquery"
to import $ from "/node_modules/jquery/dist/jquery.js"
.
- TypeScript won't be changing your import specifiers for you (e.g.
import {x} from "./x/y/z.ts"
toimport {x} from "./x/y/z.js"
) it breaks one of the founding guidelines of only erasing types (and TS would not be where it is today if it broke its own design rules) - you can write this in ESM code today and it will work. I do it.
The TypeScript compiler already rewrites import
statements depending on the value of config.compilerOptions.module
. The issue here is that when omitting a file extension from an import
statement in a source file (as has been standard practice in front-end development for a long time and is prevalent through many third-party packages in that domain), the ES2015
, ES6
, ES2020
and ESNext
settings cause the compiler to produce code that does not work on these target environments, despite tsc stating that there are no errors.
The problem isn't that our TypeScript code is wrong - it passes all compile and linting steps perfectly - it's that the compiler is doing the wrong thing when converting to JavaScript for these target module systems.
With regard to this issue, not only does Typescript use non-extension imports in it's own documentation on modules https://www.typescriptlang.org/docs/handbook/module-resolution.html the VS Code editor, which is also made by microsoft needs special settings to even work https://2ality.com/2021/06/typescript-esm-nodejs.html#visual-studio-code
I can understand sticking to a design principle. But I have to ask, WHO is this for? Who is actually benefiting from that design principle? Since it feels like, it's literally for nobody. Especially when nobody is asking to change default behavior, just a switch to append ".js" or ".mjs" when outputting (out of which there are crazier output settings already in the language). To be honest just the confusion aspect of this issue is reason enough to add a simple output
@ anyone stumbling here...
If you're trying to get your node program to work, just replace whenever you would write node
with node --es-module-specifier-resolution=node
(node v16.13.0). Think of it as the right of passage for the language; can't be mature language with out pointless mandatory flags (it's just like how you see everyone write perl -w
, welcome to the club)
If you need it as a shell header, don't forget the -S
this is the correct version:
#!/usr/bin/env -S node --es-module-specifier-resolution=node
After tediously reading 4+ years worth of comments on this thread I must say SrcSpider ended the thread on a good note for me. The thing that confuses is me, is I don't understand why no one wants to fix this issue. I don't contribute to TS, so I don't know the project all that well, but usually path resolution issues (from what I have seen) are solvable issues. At the worst case a flag could be added to change TS behavior for ES6 Module path resolution right? IDK, maybe I am out of my league, but the way it is as it stands to be extremely annoying. So annoying it makes me wonder if I am just wasting my time working to make sure I write everything in TS. The whole point of compiling to JavaScript (transpiling) is backwards compatibility, portability, and valid code (or valid ES6 JS in this case), right? Its 2022, and no one working on the project seems to even care.
@W3Dojo don't miss out part 2
https://github.com/microsoft/TypeScript/issues/16577
12 hrs of reading
I've created a pull request wich wich solve this for raltive imports, see:
https://github.com/microsoft/TypeScript/pull/47436
I've added a compiler switch:
appendModuleExtension
I've created a pull request wich wich solve this for raltive imports, see:
47436
I've added a compiler switch:
appendModuleExtension
They said it wasn't possible. Along came some guy who didn't know this and just did it. 🥳
According to my trusted sources, the rationale behind this stubborn refusal to actually make tsc
emit javascript imports with, well, .js
extension is as follows:
If implemented, Typescript newcomers will cease writhing in pangs, thus disrupting the steady supply of bad vibes that keep Ctulhu asleep, and it will make Him emerge from R'lyeh, scramble the letters in all occurrences of Microsoft in existence into Coots Firm and scuttle back into the Ancient City growling unspeakable curses; And the management clearly doesn't want that!
Let us all support the stoicism of those behind this unpopular decision and applaud their efforts to redeem the world!
I've created a pull request wich wich solve this for raltive imports, see:
47436
I've added a compiler switch:
appendModuleExtension
How exactly do I use this? I can't find anything about this option in the documentation :/
it was not accepted
I can understand sticking to a design principle. But I have to ask, WHO is this for? Who is actually benefiting from that design principle?
I can answer this for you :-). Because I am in the same boat as you, but I actually don't want this solution.. The problem with this proposed solution, is that having it available would cause a whole new host of different problems, even larger than the problem you/we are currently having.
The principle that typescript/tsc will preserve import paths, and never try to magically adjust them, is necessary for interoperability of released library code. Because import paths are never adjusted, you have a guarantee that the shape of the present import paths does not depend on specific build settings. IF typescript would adjust the paths differently based on settings, you would suddenly
Also, one of the invariant rules of typescript is, that if you feed it a plain javascript file, that file will be re-output unmolested/without needing any adjustments. If you had this import-path adjusting possibility, it would need to be able to adjust import paths in a plain javascript file, violating the guarantee that it won't adjust plain-javascript.
I/we are already living variants of this nightmare. I am currently trying to use an npm package that contains javascript files that use weird import syntax. Because of this, I cannot use that npm package with browser-ESM imports, because the foreign-import-syntax in that npm package is not legal browser-ESM. Neither can I use that npm package directly in nodeJS, because the foreign-import-syntax is not legal nodeJS either. Instead, I am forced to go on a detective-mystery-hunt, to guess-figure-out which magic combination of bundler tools the authors of that npm package are using, to allow their fancy non-standard import syntax to work.
This is the hell these 'defenders of typescript' are fighting against/protecting you from :-) :-(.
I will now continue on my odyssey to unravel, how their (my 'enemies'.., not the ts-people) non-standard import-syntax can be made to work when using ESBUILD :-/.
Random addition: one solution to this for browser native ESM is to add a service worker to your app, and append the .js
extensions in the service worker. I've had good success with that (although I much prefer just adding .js
in the import statements and simply moving along while keeping complexity lower).
A first implementation of
<script type="module"><script>
just landed in Safari Technology Preview 21, so I tried to use it on a TypeScript / Angular project, to finally get rid of system.js.First issue, resolved but unconvenient : ES6 modules paths must include the
'.js'
extension. Fortunately, TS 2.0+ knows how to resolveimport { Something } from './test.js'
intoimport { Something } from './test.ts'
while in dev. But it's a huge change to common practices and a lot of refactoring : official TypeScript docs, Angular, RxJS and so on have all encouraged until know to use the no extension form (import { Something } from './test'
).Second issue, unresolved : TS provides many options to tell the compiler how to resolve paths like
import { Component } from '@angular/core'
. But it never transforms the imported path in the compiled files.'@angular/core'
will stay'@angular/core'
in the transpiled files. After investigation, I've read in many issues it is by design.It's OK with loaders like system.js and others, which provide similar path mapping options. But with
<script type="module"><script>
, as far as I know, there is no configuration, soimport { Component } from '@angular/core'
will always fail.I am missing something, or does that really mean that we won't be able to use the native loader with TypeScript projects ?
If I'm right, is it possible to add an option to force path transformation in compiled files ?