Woozar
Woozar

Reputation: 1130

How to build a NestJs api from a mono repository

I am working on a project where we are storing our code in a monorepo that is "managed" by nrwl. The repo contains a couple angular apps and a couple nestjs apis.

Now we want to dockerize the apis to be able to deploy them to our kubernetes.

I can start a single api by calling ng serve api1. I can also build a single api by running ng build api1. The problem I've got is that every api needs the full node_modules folder deployed with it. As far as i know angular uses webpack to pack all its depedencies.

Is there a way how to build the NestJs api and include all required node_modules?

Upvotes: 2

Views: 5107

Answers (1)

Mark Pieszak - Trilon.io
Mark Pieszak - Trilon.io

Reputation: 67151

We have actually just released a native way to create monorepos with NestJS, you can read more about the full announcement here on the Trilon blog

With a standalone NestJS application, you can now simply create (another application, or library) and automatically have it turned into a monorepo workspace.

# make sure you have the latest @nestjs/cli
npm i --g @nestjs/cli

# if you want to create an app
nest g app my-other-app

# library
nest g lib my-library

Now you will be able to use nest build NAME to build whichever application you need, and all node_modules etc will be bundled correctly, etc!


This new workspace setup will create a naming convention you're used to:

# ** FROM

 /src/ ... application code ...

# ** TO

/apps/
  /nest-app/
  /another-app/

/libs/
  /some-shared-lib/
  / ... /

The way monorepos are setup in Nest currently is ideal for monorepos that are only nest/node codebases. Frontend monorepos could be held separately somewhere else, as there is limited code (only some interfaces without decorators) that could be shared between FE<>BE.

Hope that helps :)

Upvotes: 3

Related Questions