Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If set type: module package.json, ts path-alias not replace, and can not resolve alias path module. #10267

Closed
4 of 15 tasks
Innei opened this issue Sep 11, 2022 · 6 comments
Closed
4 of 15 tasks
Labels
needs triage This issue has not been looked into

Comments

@Innei
Copy link

Innei commented Sep 11, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Can't replace alias path if file in nested-folder, but in root dir such as /src replace correctly.

import { Module } from '@nestjs/common';
import { AppController } from '~/app.controller.js'; // right
import { AppService } from '~/app.service.js'; // right
import { TestModule } from '~/test/index.module.js'; // wrong

transform to:

import { Module } from '@nestjs/common';
import { AppController } from "./app.controller.js";
import { AppService } from "./app.service.js";
import { TestModule } from '~/test/index.module.js'; // not replaced!!!

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-pnhuer?file=dist/app.module.js&terminal=start

Steps to reproduce

npm start:dev


Error [ERR_MODULE_NOT_FOUND]: Cannot find package '~' imported from /home/projects/nestjs-typescript-starter-pnhuer/dist/app.module.js
    at InternalError.get (https://nestjs-typescript-starter-pnhuer.w.staticblitz.com/blitz.a1e315a41ec6498e2c2758b32fbf437db67b244b.js:6:292488)
    at process.emit (/home/projects/nestjs-typescript-starter-pnhuer/node_modules/source-map-support/source-map-support.js:508:52)
    at process.onGlobalUncaughtException [as _fatalException] (https://nestjs-typescript-starter-pnhuer.w.staticblitz.com/blitz.a1e315a41ec6498e2c2758b32fbf437db67b244b.js:6:251442)
    at Object.triggerUncaughtException (https://nestjs-typescript-starter-pnhuer.w.staticblitz.com/blitz.a1e315a41ec6498e2c2758b32fbf437db67b244b.js:15:356815)
    at i.loadESM (https://nestjs-typescript-starter-pnhuer.w.staticblitz.com/blitz.a1e315a41ec6498e2c2758b32fbf437db67b244b.js:6:246694)
    at handleMainPromise (https://nestjs-typescript-starter-pnhuer.w.staticblitz.com/blitz.a1e315a41ec6498e2c2758b32fbf437db67b244b.js:6:989050)

Expected behavior

replace tsc alias path.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

@nestjs/cli

NestJS version

9.0.11

Packages versions

 "@nestjs/cli": "9.1.2",

Node.js version

v16.14.2

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@Innei Innei added the needs triage This issue has not been looked into label Sep 11, 2022
@jmcdo29
Copy link
Member

jmcdo29 commented Sep 11, 2022

Looks like tsconfig-paths (what Nest is using under the hood for replacing path aliases) doesn't support ESM. There's a number of open issues in their repo about it.

@Cristiandi
Copy link

so what it means is, when tsconfig-paths support ESM nestjs will be able to work with ESM with no problems? is that right?
hehe sorry for the silly question 😅

@jmcdo29
Copy link
Member

jmcdo29 commented Sep 11, 2022

so what it means is, when tsconfig-paths support ESM nestjs will be able to work with ESM with no problems? is that right? hehe sorry for the silly question sweat_smile

Partially, there's still more to it than just tsconfig-paths. There's ensuring we can still support express, fastify, apollo, mercurius, all the microservice transports and WS, and ensuring that unless we upgrade at a major version we don't introduce any breaking changes for CJS users as well. Not to mention testing tools supporting ESM and migrating the entire code base over to ESM as well.

In short: it's something we're working on and monitoring, but it's gonna take a while

@Cristiandi
Copy link

Buffff that seems to be a huge n important, anyway thanks a lot for working on it and on this amazing framework, gonna be expecting the ESM compatibility changes.
Thanks for your explanation 💪🏻

@yuntian001
Copy link

I had the same problem. How did you solve it

@gterras
Copy link

gterras commented Apr 30, 2024

I had the same problem. How did you solve it

That's the neat thing about esm/cjs interop, you don't. Most likely your situation can be solved another way like changing an extension to .mjs.

Worth noting that https://github.com/privatenumber/tsx is often mentioned in https://github.com/dividab/tsconfig-paths issues, it looks like a viable alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

6 participants