Skip to content

Support ESM typescript modules #46

@achingbrain

Description

@achingbrain

If your tsconfig.json has "module": "ES2020" or later (e.g you are outputting ESM and not CJS), TypeScript doesn't rewrite import './foo' to import './foo.js' in the transpiled output - instead you have to use import './foo.js' in your .ts file.

This module then breaks because it (quite reasonably) detects './foo.js' as a dependency which of course doesn't exist.

I've hacked round it by replacing the file extension here and here but it doesn't take into account if ./foo.js does actually exist and that's what you meant to import. I don't know the AST well enough to make that change, but a simple fs.existsSync should be enough?

Another workaround is to run the dep checker over the transpiled output instead of the ts source, but then you lose any import type declarations, so it's not ideal.

Refs: https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#type-in-package-json-and-new-extensions

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions