question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature: disable extensionless imports

See original GitHub issue

I’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:open
  • Created 5 years ago
  • Reactions:28
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

21reactions
fatcerberuscommented, Nov 10, 2018

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:

  1. Automatic imports are added without an extension
  2. Browsers don’t support extensionless imports
  3. TS doesn’t support module specifiers rewriting

…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.

0reactions
thw0rtedcommented, Dec 14, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found