Skip to content

Adding support for ESM references without a .js extension  #46006

Closed as not planned
@staplespeter

Description

@staplespeter

What is the problem this feature will solve?

When running JS files output from tsc without explicit .js extensions on implicit .ts module imports, Node.js produces an error.

File X.ts:
import A from './Y';

File Y.ts:
export default const A = 10;

tsc output is to X.js and Y.js with content being identical. node X.js produces the following:
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'Y' imported from X.js

File X.ts must contain:
import A from './Y.js';
for this import to work.

It seems there has been much discussion on this for many years, with the TypeScript team refusing to add support for File X.ts:
import A from './Y';
transpiling to File X.js:
import A from './Y.js';

See

In places the TypeScript dev team have insisted that Node.js should be resolving the ESM dependencies correctly. Frankly either tsc or Node.js can resolve this issue. Doing so would really help A LOT of people. It would be good to see some collaboration between TypeScript and Node.js teams to make this ESM support work cleanly.

What is the feature you are proposing to solve the problem?

Add module resolution for ESM imports without an explicit .js extension when imports are within the package.

What alternatives have you considered?

Crap workarounds:

  • Using .js extension in all imports
  • Renaming output files post-transpilation

A seamless solution for the toolchain is what is required. Current solutions are improper, or are hacks, or leave the burden on the dev to effectively manually add the .js extensions. These are programs, they are meant to do the grunt work for devs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateIssues and PRs that are duplicates of other issues or PRs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions