luanglopes / ts-paths-esm-loader

Node JS loader for Typescript that supports tsconfig paths
MIT License
23 stars 5 forks source link

Monorepo cross-package imports not working #11

Closed bnason closed 3 months ago

bnason commented 1 year ago

When using a pnpm monorepo where multiple projects are using ts-paths-esm-loader and project a imports from project b, I believe the paths from b are being resolved relative to projects as path. I've created a demo project here: https://stackblitz.com/edit/json-server-dv21mg?file=packages/b/src/index.ts

If you open the terminal and go into packages/a and run pnpm start you will see the error below. However, if you switch the export to the one that uses the relative path and rerun pnpm start, it works correctly.

> node --no-warnings --loader ts-paths-esm-loader --experimental-specifier-resolution=node --experimental-fetch src/index.ts

TSError: ⨯ Unable to compile TypeScript:
src/index.ts:1:10 - error TS2305: Module '"@test/b"' has no exported member 'server'.

1 import { server } from '@test/b';
           ~~~~~~

    at [object Object]
    at [object Object]
    at [object Object]
    at [object Object]
    at [object Object]
    at [object Object]
    at [object Object]
    at [object Object]
    at [object Object] {
  diagnosticCodes: [ 2305 ]
}
 ELIFECYCLE  Command failed with exit code 1.

It seems like this is also a problem in tsconfig-paths https://github.com/dividab/tsconfig-paths/issues/153

luanglopes commented 1 year ago

Hello @bnason, I don't have experience with pnpm at all, so I don't know how pnpm workspaces works, but if I'm not wrong, as you are running pnpm start inside package/a and pnpm start is basically node --no-warnings --loader ts-paths-esm-loader --experimental-specifier-resolution=node --experimental-fetch src/index.ts, ts-paths-esm-loader will only work for files inside packages/a, and @test/b will be considered an "external" module, so it will not do any transformation on it.

So, as it is pnpm workspaces that "maps" your packages between each other, maybe the solution for you is to find a way to add ts-paths-esm-loader as a loader for pnpm workspaces mapping