How to Resolve Inability to Reference Internal Packages from Turborepo NestJS
Learn how to resolve the issue of being unable to reference internal packages (such as Prisma) from Turborepo NestJS.
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
"C:\Program Files\nodejs\npm.cmd" run start
> nest@0.0.1 start
> nest start
Error: Cannot find module '@repo/database'
Require stack:
This issue can be circumvented by compiling internal packages to JavaScript before importing.
Solution
1. Install tsup in the Internal Package
npm i -D tsup -w <workspace>
e.g. For an internal package named data-access-db (assuming it uses PrismaClient):
npm i -D tsup -w packages/data-access-db
2. Modify the Internal Package's package.json
Configure the build command to export JavaScript files to the dist folder.
package.json
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts",
"dev": "npm run build -- --watch"
},
3. Build & Install the Internal Package Before Starting NestJS Project
npm run build -w <workspace>
npm i
e.g.
npm run build -w packages/data-access-db
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.
Related plants
More Tech articles →Setting Up Nx Nestjs Mongodb Mongoose Integration - Part 3
Part 3 of Nx Nest.js GraphQL Integration - Install packages, update configs, generate GraphQL client for seamless integration. Enhance data management and scalability. Congrats on completing this guide to harmonious Nx, Nest.js, GraphQL integration! Stay tuned for more tutorials.
#nx#nestjs#mongodbSetting Up Nx Nestjs Mongodb Mongoose Integration - Part 2
Part 2 of Nx Nest.js MongoDB Mongoose Integration - Generate todos, create modules, implement GraphQL models, repositories, DTOs, and MongoDB schemas for enhanced application efficiency. Stay tuned for a comprehensive configuration guide.
#nx#nestjs#mongodbSetting Up Nx Nestjs Mongodb Mongoose Integration - Part 1
Guide to integrating Nx, Nest.js, MongoDB, and Mongoose for enhanced scalability and efficiency. Follow steps for seamless development.
#nx#nestjs#mongodb