Nrwl nx command to generate lib for nest module
Asked Answered
G

4

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.

Genro answered 28/3, 2019 at 12:33 Comment(0)
G
10

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

ng generate @nestjs/schematics:library mynestlib
Genro answered 28/3, 2019 at 12:52 Comment(5)
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.Urbain
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 appGenro
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: github.com/nrwl/nx/issues/1010Swindle
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 github.com/nrwl/nx/blob/master/docs/angular/api-nest/schematics/…Parliamentarianism
You can also install the nest plugin for nx nx.dev/angular/plugins/nest/overviewMonjan
U
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"
},
Urbain answered 29/3, 2019 at 9:2 Comment(3)
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 NestJSGenro
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.Urbain
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 saidGenro
C
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 :)

Corbie answered 7/2, 2022 at 23:7 Comment(0)
T
0

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

Thought answered 28/5, 2022 at 20:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.