Feature: disable extensionless imports
See original GitHub issueI’d like there to be a way to always require the file extension to be specified for relative import statements. At the moment extensionless imports always just work. Even ‘Go to Definition’ just assumes it should open the JS file.
src/
- index.js
- foo.js
// index.js
import { Foo } from './foo'; // extensionless
I’d like there to be a jsconfig option to disable this behavior and require a file extension to be specified.
import { Foo } from './foo';
^^^^^^^ No definition found for 'foo'
// fix
- import { Foo } from './foo';
+ import { Foo } from './foo.js';
I’m asking because extensionless imports don’t really work in the browser if you’re not using a module bundler. It can only work if the HTTP server supports extensionless imports, which is a big ask.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:28
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Is it possible to make Node NOT Require the ".js" Extension ...
Node now has built-in support for imports, which is awesome. But that support requires you to specify the file extension, which is annoying....
Read more >find-unused-exports - npm
The npx find-unused-exports script is handy for finding redundant code to remove in legacy projects. Use the CLI command find-unused-exports ...
Read more >Core Features - Spring
This section dives into the details of Spring Boot. Here you can learn about the key features that you may want to use...
Read more >tsc-esm-fix - npm Package Health Analysis - Snyk
Features. Injects extensions to imports/re-exports statements. import {foo} from '. ... --unlink, Remove original files if ext changes, true.
Read more >Using ES modules in browsers with import-maps
We will not focus on CommonJS unless it has a direct feature to ES ... if you want to adopt the extension-less strategy...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m going to put my support behind seeing a fix for this. Browsers support ES modules natively now and TS isn’t a nice experience for web development because:
…which creates a perfect storm in which it’s possible to write several modules with a nontrivial dependency graph before realizing that the whole thing is broken because none of the import statements have extensions. Fixing it is then an exercise in patience as you hunt through the project looking for offending imports—and the browser won’t help you here because it stops short at the first one that fails to load. It’s not a good development experience, which sucks because TS is so, so nice otherwise. 😦
Even if TS just rewrote imports in the generated code to append a
.js
to any specifiers without it, that would be a good interim solution, I think.It’s weird but I don’t think this issue has any links yet to the ur-issue, https://github.com/microsoft/TypeScript/issues/16577. Technically, providing a flag to error out is different from providing a flag to append the correct extension, but it’s pretty similar.