10

I am new to nrwl nx cli and extensively searched through their documentation and could not find the command to generate the nestjs module as lib rather than the actual module within app.

The reason why I want to create the nestjs module as lib is becuase I have got several nestjs applications in my mono repo and as per nrwl nx guidelines if I have to share code, which is module in my case, it has to be a lib.

Can anyone please share the command to do the following thing:-

  1. Generate nestsjs module in libs
  2. Create nestjs service for that module in libs folder

Any help is much appreciated.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Atul Chaudhary
  • 3,698
  • 1
  • 31
  • 51

4 Answers4

10

I got the command that does the job finally and sharing with other encase they require help

ng generate @nestjs/schematics:library mynestlib
Atul Chaudhary
  • 3,698
  • 1
  • 31
  • 51
  • with recent version of nx, you can use "ng g lib my-nest-lib", you'll be prompted for parent folder, and angular or typescript lib for example. – Thierry Falvo Mar 28 '19 at 19:42
  • 1
    Thanks for the reply Thierry Falvo but if I use that command it wil never ask for nestjs and won't scaffold the nestjs module and app – Atul Chaudhary Mar 29 '19 at 08:19
  • I would not recommend using `@nestjs/schematics:library` directly as it does not update `nx.json`, `angular.json`, etc. properly. You'd have to do it manually each time. Please upvote this feature, if still interested: https://github.com/nrwl/nx/issues/1010 – demisx Nov 30 '19 at 16:01
  • 3
    It's possible now to create a NestJS library including controller and service inside a Nx library: `ng g @nrwl/nest:library my-lib --controller --service`. See https://github.com/nrwl/nx/blob/master/docs/angular/api-nest/schematics/library.md – Bernhard Fürst May 01 '20 at 15:54
  • You can also install the nest plugin for nx https://nx.dev/angular/plugins/nest/overview – Tucaen Jul 26 '20 at 12:10
1

Quote from Nx Workspace official documentation :

Creating a Lib Adding new libs to an Nx Workspace is done by using the Angular CLI generate command, just like adding a new app.

ng generate lib mylib
ng generate library mylib # same thing

This will create a new lib, will place it in the libs directory, and will configure the angular.json and nx.json files to support the new lib.

Run ng generate lib --help to see the list of available options.

ng generate lib mylib --directory=myteam will create a new application in libs/myteam/mylib.

If --directory is not defined, it will ask you in which directory it should be generated, and what framework should be used (Angular or TS).

If it's not working, please, maybe you should check your Nx version, and also your angular.json.

@nrwl/schematics could not be the default collection used as this issue mentioned.

angular.json

"cli": {
  "defaultCollection": "@nrwl/schematics",
  "packageManager": "yarn"
},
Joel
  • 678
  • 8
  • 17
Thierry Falvo
  • 5,892
  • 2
  • 21
  • 39
  • 2
    I understand what you saying and checked what you have told but what I am saying is when you create library using the ng g lib command, it only give options of Angular, React and Typescript framework only but nothing for NestJS – Atul Chaudhary Mar 29 '19 at 11:38
  • Sorry, I misunderstood what you was looking for. But maybe you should not use "generate lib" from NestJS because it won't add needed Nx config in angular.json, and all tsconfig files. So after generating a TS lib with Nx, you can generate module and service with NestJS. Hope it can help you. – Thierry Falvo Mar 29 '19 at 16:06
  • Thanks for the advice mate, that is what I am doing now. it is bit of pain but I guess that is the only option as using command mentioned above does not update the nx.json and angular.json file as you have said – Atul Chaudhary Mar 30 '19 at 11:09
1

I've came across the same problematic lately.

While @Atul Chaudhary can solve the issue in a different way, I could solve my problem following this comment

So basically, you just have to add a new nest-cli.json file to your nx workspace root folder and specify these basic informations according to your project's structure.

nest-cli.json basic example

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "apps/api/src"
}

Hope it can help some people :)

A. Maitre
  • 2,985
  • 21
  • 25
0

Nx now has direct support for NestJS via their @nrwl/nest plugin. They also have this tutorial that you may find useful.

wrslatz
  • 403
  • 2
  • 8
  • 25