Skip to content

tsconfig-paths does not work with ESM; possible switch to tsc-alias #2858

@dyedwiper

Description

@dyedwiper

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

tsconfig-paths is used by the nest-cli to rewrite paths according to path aliases specified in tsconfig.json.

tsconfig-paths does not work with ES Modules as reported here e.g.:

Thus nest start throws an ERR_MODULE_NOT_FOUND when compiling to ES Modules and using path aliases. Here is a minimal example: https://stackblitz.com/edit/nestjs-typescript-starter-m27m4jmo

It is related to this issue: #1437. Though AFAIU the problem is not caused by the file extension. Thus I decided to open a new issue. Hope that's fine.

Describe the solution you'd like

A solution could be to use tsc-alias as a post-compilation step, instead of using tsconfig-paths in pre-compilation.

A possible benefit: Rewriting the paths with tsconfig-paths in pre-compilation involves some custom code which you could probably lose with tsc-alias.

I haven't fully tested it, but when I change the build script to "nest build && tsc-alias -p tsconfig.build.json", the import is correct in the compiled dist/app.module.js after running npm run build. See here: https://stackblitz.com/edit/nestjs-typescript-starter-rcmvukfp

Teachability, documentation, adoption, migration strategy

No response

What is the motivation / use case for changing the behavior?

We would like to use path aliases in a project and this hinders it at the moment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions