-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation on Modules is missing ".js" suffix in import statements #27287
Comments
@griii2 I think there's a couple of things going on here:
|
Hi @bcherny, I am not sure I understand what you mean. Of course I compiled the TypeScript files. No, I am not running them in Chrome. What I run in Chrome is an HTML5 file that loads the compiled .js files. My point is this: 1/ I am reading the documentation: https://www.typescriptlang.org/docs/handbook/modules.html |
@griii2 Sorry, I don't know what you know or don't know - always better to check :) See what I said above - is this your intention?
|
I see, you are saying the compiler should work the way the documentation says. I will raise the feature request. But as things stand today, the documentation does not match the implementation, so I suggest that until the feature request is released the documentation should be fixed to describe the implementation. |
The exact resolution of module specification -> file is up to the host; it is likely that nothing we write will work in every possible host. The examples work when targeting CJS compilation, which is still the default and is widespread. |
Hi @RyanCavanaugh: a) If the example works only when targeting CommonJS then the documentation should not imply it works for all module targets. b) As a consequence, module importing targeting ES6 is not documented today. Anyway, this was my feedback to the TypeScript team because I care. If you want to be defensive instead of fixing what is broken, that is up to you. Best |
I'm having the same problem, but succinctly, it's that when I compile
and target ES2015 or ESNext modules, it gets transpiled as:
Yes, exactly the same thing. Since I'm targeting javascript, it should be adding the Smells like a bug to me. I'm on Typescript 3.1.3. |
As an aside, this is handled in SystemJS with a |
I have the same problem: When targeting "es6" then |
@oising It's not really about adding extension, but expanding the full relative path during compilation. 1. What you imported may not be a file but folder.import { Foo } from './module' Might also result to: import { Foo } from './module/index.js' The path cannot be determined by the entry, but require the full path of the dependency. 2. The target JavaScript file compiled from TypeScript may not be
|
@trotyl Sure, it's complicated with Node (the majority of your examples), but if you're just wanting to write JS modules that directly target browsers - without using bundlers like webpack or parceljs, it shouldn't be that hard. |
@oising All of the points above are problems for not using Node.js or any bundler. |
It would be great if we could just have the option of being able to specify something like If typescript was to be used to do this resolution ahead of time, it could be powerful for a lot of reasons. For example given this directory:
This could be resolved universally as when transformed into
This is going to work universally because it sill follows a resolution process that is compatible with all engines, I don't believe there is anything lost by doing so, the option on
This would be a semver minor change as well. This can be extended though, and this is where it gets powerful. Lets say we defined it as:
Then when we reference |
Hello, i encountered the same problem, and i SOLVED "easily", this way : URL REWRITING, what do i mean ? i'm gonna explain that from the beginning ! Here is the story, from compiled Typescript code, i wanted to obtain some ES6 code(target es2015 in tsconfig.json), in order to keep in the final code the original syntaxes: import/export that i've written in Typescript ( indeed at final, i did not want to use Module loaders for ES5 !! Because it's old school...). The thing is that, when Typescript generates the ES6, it completely keeps intact the syntaxes : import .. from "./.../MyJSFile", so it keeps it without the ".js" extension !! And of course, in Typescript code, you can't mention the extension on an import syntax ! (it assumes it to be ".ts"). So now, i've got like you, the ES6 code generated, and containing import syntaxes that DOESN'T mention the ".js" extension of the imported files. So what is the problem ? The problem is :
YOU JUST HAVE TO WRITE : URL REWRITING RULES FOR THE WEB SERVER, and that's
AND THAT's really all you have to do ! with an Apache Server. I ALSO USED THE SAME PRINCIPLE WITH a NODE.JS WEB SERVER that i've created, here is its full tested code, it works absolutely fine (nodeJsAsWebServer.js) :
|
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow. |
I'm also running into this issue; I think it's plainly almost unimaginable that a simple .ts file using modules and targeting modern browsers and ES modules cannot be compiled to a working javascript file in the browser. I'm using the workaround suggested by others in the related thread (using an explicit .js module reference in the typescript file, like "import {Foo} from './module.js' ", but this feels rather strange, although it works well in the browser. |
This would make a good question for stack overflow if the feature were supported. As a workaround, @RickInfoDEV's satirical answer isn't that bad especially if the typescript people will never support Web Modules |
TypeScript Version: 3.1.0-dev.201xxxxx
Search Terms:
import, module
Code
When using the import declaration from the documentation
import { ZipCodeValidator } from "./ZipCodeValidator";
the latest Chrome will not find the 'ZipCodeValidator' file. The console will show an error "Failed to load resource: the server responded with a status of 404 (Not Found)".
This can be easily fixed by adding the ".js" suffix to the imported module.
import { ZipCodeValidator } from "./ZipCodeValidator.js";
Expected behavior:
Module should be imported.
Actual behavior:
Latest Chrome returns "Failed to load resource: the server responded with a status of 404 (Not Found)"
Playground Link:
Related Issues:
The text was updated successfully, but these errors were encountered: