-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
import without extension #30927
Comments
Hey - thanks for the report! I think you misread this part:
What this means is files that don't have an extension (e.g. if |
Thanks for the quick response. That's a bit different from the behavior in commonjs but no biggie. I'm closing the issue. |
The docs should probably make this clearer. I can see how someone would be confused. |
Thanks for trying out this feature, feedback is super valuable at this point. Please let us know if there's any other issues you're running into! |
Turns out that people are really confused with this feature : https://stackoverflow.com/questions/56723678/node-experimental-modules-error-cannot-find-module |
Is that still the case? This ES modules spec suggests that a list of extensions is automatically tried (via this SO question). |
That is the case. The spec you linked to is three years outdated. Please reference https://nodejs.org/api/esm.html in the future. |
In manual there is a specific topic about it: https://nodejs.org/api/esm.html#esm_mandatory_file_extensions |
That link doesn't really explain anything. It just says:
Well great ... but if we're not in a browser environment: why force us to use |
Because ESM modules is a feature available in browsers too, and you could someday to try to use your module there... and in will crash. Maybe is a thing that should be dictated by the spec. |
@piranna no, it wouldn't. browsers don't have the concept of "extensions". You'll just need to ensure (as you always would) that the URLs work - for ESM, either with import maps, or by teaching your server how to map the nice clean paths into a file on the filesystem. |
I just want to point out that there are two separate things here. One is the Node org making a call (right or wrong) on how to handle extensions ... and the other is the Node org making a call whether or not to explain that decision. Maybe the Node org made the right call on extensions ... but to an outside observer, it's impossible to tell, because the Node org refuses to explain why they made that call. In this thread I think all anyone is asking for (or at least all I'm asking for) is for the Node org to update the documentation to explain why this (clearly unpopular) choice was technically necessary ... if it truly was. |
It was not technically necessary at all; the desire as i understand it was to match closer with browsers (ie, 1:1 specifiers to lookups, no helpful/magic resolution logic), and to make it easier for tooling to translate a node dependency graph to browser import maps. The latter, however, doesn’t apply anymore since there’s custom conditions, and subpath patterns, not to mention the vast majority of packages still being CJS without an exports field, but here’s where we are. |
No decision has been made. That's why I don't expect anything to change until work on loaders leaves experimental status. At that point it should become clear if Node needs to provide a built-in way to resolve extensionless specifiers or if that's something that could be reasonably achieved via external solutions. |
I've been trying to get es6 imports to work. why is in in guides and tutorials the code usually uses the import syntax without extensions on the files but it seems to still work? |
@MrBrN197 because everyone in those examples is using Babel or TypeScript, not native ESM. |
@ljharb I see. I've just started and I'm trying not to get into Babel. but I use typescript. so typescript compiles to es6 and uses extensions on modules imports. |
Typescript uses Babel under the hood. |
I don't believe that's true - |
In addition to the above, at the very least, node needs to add more helpful error messaging. Adding As such, I created an issue for this at #38484 |
Hello! And what about index scripts? Now we must do |
I don't understand why would anyone decide to change the behavior everyone is already used to and which works for everyone... |
Set |
@AnderAmorim You don't actually need type module for that; you could also rename |
Avoid .js extension in relative imports nodejs/node#30927 (comment)
this worked for me as well. Thank you @AnderAmorim! |
How we do in node 19. They have removed the experimental option. I cannot found easily a replacement. |
My solution is to run it with Node.js v18 using |
I investigated on node 19: here is my solution. Use a custom loader (node --loader). It is experimental so you get a warning. Use --no-warnings to disable the experimental warning. Then I had to write a custom loader script in order to get the .js extension added in certain cases.
Here is the script of the esm-loader.js export const resolve = (specifier, context, nextResolve) => {
if (specifier.startsWith("./") || specifier.startsWith("../")) {
if (
!(
specifier.endsWith(".js") ||
specifier.endsWith(".mjs") ||
specifier.endsWith(".cjs")
)
) {
const newSpecifier = specifier + ".js";
return nextResolve(newSpecifier, context);
}
}
return nextResolve(specifier, context);
}; You can see it working on my educational project here: https://github.com/jlg-formation/njs-mars-2023/tree/50eaf33a3e2d229e3dcb239bfd26e95ba92aae00 |
I'm glad we have a custom loader. We are not suppoto break stuff in the mean time though. It is hard enough to find people to maintain all these libraries out there. Features need to keep working if this is going to be a good investment for developers to use node and JavaScript. |
What is going on with Node.js? Why has it become a nightmare to use this framework? Now the latest: I have to now go through tens of thousands of files and change their imports to use a |
I'm using TS for an NPM package that has an executable. I compile with |
Here's a slightly more extended version of jlguenego's importer in the comment above, which has been working for me for a few months, and restores extension-free import while the various projects are still fighting it out to see who will blink first. Put this in
|
According to the v 13.x API docs The "type" field defines how .js and extensionless files should be treated within a particular package.json file’s package scope. and The package scope applies not only to initial entry points (node my-app.js) but also to files referenced by import statements and import() expressions.. However, imports without extensions do not seem to work:
Thanks
The text was updated successfully, but these errors were encountered: