When utilizing internal packages in a Turborepo project, frontend projects like Next.js function smoothly and can utilize internal packages without issues thanks to the transpilePackages
configuration option.
However, backend projects like NestJS encounter errors when attempting to import internal packages directly.
e.g. error
This issue can be circumvented by compiling internal packages to JavaScript before importing.
Solution
1. Install tsup
in the Internal Package
e.g. For an internal package named data-access-db
(assuming it uses PrismaClient):
2. Modify the Internal Package's package.json
Configure the build command to export JavaScript files to the dist
folder.
package.json
3. Build & Install the Internal Package Before Starting NestJS Project
e.g.
As rebuilding the internal package every time you start NestJS is cumbersome, it's advisable to prepare a script in the top-level package.json
to build and install internal packages before starting the NestJS project.