Description
TypeScript Version: 4.0.3
Search Terms: module es6 es2015 import file extension missing js ts bug 404
Steps to reproduce:
Create a main.ts
:
import {foo} from './dep';
console.log(s, foo);
Create a dep.ts
:
export const foo = 42;
Create a tsconfig.json
:
{
"compilerOptions": {
"module": "ES2015"
}
}
Then run:
npx tsc
Expected behavior:
The compiler generates JavaScript "which runs anywhere JavaScript runs: In a browser, on Node.JS or in your apps" (according to the TypeScript homepage). For example, it would be valid to create two files as follows; main.js
:
import { foo } from './dep.js';
var s = "hello world!";
console.log(s, foo);
And dep.js
:
export var foo = 42;
(More generally, the expected behavior is that the module specifier in the generated import
must match the filename chosen for the generated dependency. For example, it would also be valid for the compiler to generate a file dep.xyz
, if it also generated import ( foo } from './dep.xyz'
.)
Actual behavior:
As above, except that in main.js
, the import
URL does not match the filename chosen by the compiler for the generated dependency; it is missing the file extension:
import { foo } from './dep';
When executing main.js
in the browser, it requests the URL ./dep
, which is a 404. This is expected, as the correct relative URL would be ./dep.js
.
Related Issues: #13422, voluntarily closed by the reporter for unknown reasons