Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestJS Monorepo project - Can I build a library separate?

I have a NestJS Monorepo app I created using the nest CLI. Its a main app and I created a library called "common".

I would like to package the library up and distribute it within my company but when I do, it won't compile correctly.

I tried running nest build common but that just ended with a dist/libs/common/index.js with only about 100 lines in it. Nothing in that webpack file looks remotely like my library code.

What am I doing wrong? Building the overall app works fine. I just can't figure out how to get the individual library to build.

like image 899
SpaceCowboy74 Avatar asked Dec 09 '25 08:12

SpaceCowboy74


1 Answers

No, not this way.

Monorepo with Nest CLI

The approach with existing Nest CLI monorepo is to have a code repository with all the shared libraries and applications in it, where libraries are supposed to be published with the applications; not individually to any registry. The libraries are bundled with the application in one file.

You can have multiple apps though, which can use these libraries as required, and published individually.

For example: if you have myApp1 and myApp2, specify the app name with yarn build, and webpack will create one single file with all the dependencies in it.

Something like yarn build myApp1 or yarn build myApp2

If you check the dist (build) folder, and the individual application build file in their subfolders. Webpack bundles everything in one file.

(Your) Use case - Create one code repository and publish multiple apps and libraries individually

Use specialized tools for that:

  1. Lerna - more flexible and requires extra customization
  2. Yarn Workspace - gets the job done as well

These are more efficient when managing complex projects. Here you have all the fancy features you need, like caching, version control etc. and lighting fast speed!

like image 154
sandiejat Avatar answered Dec 12 '25 00:12

sandiejat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!