Skip to content

Support .js extensions. #783

Closed
Closed
@MicahZoltu

Description

@MicahZoltu
import { ... } from './foo.js'

Expected Behavior:

This should compile by resolving the import to ./foo.ts.

Actual Behavior:

Errors complaining that it cannot find ./foo.js.


  • ES2018 module loaders do not do any extension inference.
  • TypeScript compiler does not append any extension to import statements in generated files.
  • TypeScript will infer ./foo.ts from ./foo.js during compilation.

In order to write TypeScript that is compatible with ES2018 module loaders, you must therefore do import ... from './foo.js'. TypeScript compiler will see this import and guess that you mean foo.ts or foo.d.ts. However, ts-node does not and instead will error saying it cannot find foo.js.

While my personal opinion on this matter is that the inability for ES2018 module loaders to append a default file extension to packages that have no extension is unfortunate, it is none the less the situation we are currently in. The hope is that eventually the TypeScript compiler will be able to be given a flag that tells it to add .js when none is present, thus allowing us to write import ... from './foo' and have that generate JS like import ... from './foo.js', but that day is not yet here.

Related issues:
microsoft/TypeScript#16577
microsoft/TypeScript#28288

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions