Description
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
- TypeScript cannot emit valid ES modules due to file extension issue microsoft/TypeScript#42151
- Provide a way to add the '.js' file extension to the end of module specifiers microsoft/TypeScript#16577
- bug(esm): TypeScript is not an ECMAScript superset post-ES2015 microsoft/TypeScript#50501
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.