1

enter image description here Context

[I am new in this field]

I am working on Angular + Nestjs project and I have created new server directory using nest new server and selected yarn as my package manager,

Problem

I then moved into this directory and ran nest generate route cats to scaffold new route directory with necessary resources, but in response I get an error which says: Invalid schematic "route". Please, ensure that "route" exists in this collection.

Read this before you answer

I made sure that I was in root directory of my nest project and there was no typing error.

I also checked for devDependencies in package.json file where "@nestjs/schematics": "^9.0.0", is present.

Can any one tell why am I getting this error and how to fix it? I searched on the internet regarding this but got no satisfactory answer.

Alfha
  • 117
  • 9

2 Answers2

1

I think you're meaning to run nest generate resource cats. There's no route schematic, but there is a resource which can be used to generate CRUD operations


Edit

Below is the output of nest g --help. Notice there is no route schematic. Whatever you're looking at that shows a route schematic is either custom, or wrong information

docker run -it node sh
# npm i -gs @nestjs/cli             
npm notice 
npm notice New minor version of npm available! 9.5.0 -> 9.6.2
npm notice Changelog: https://github.com/npm/cli/releases/tag/v9.6.2
npm notice Run npm install -g npm@9.6.2 to update!
npm notice 
# nest g --help
Usage: nest generate|g [options] <schematic> [name] [path]

Generate a Nest element.
  Schematics available on @nestjs/schematics collection:
    ┌───────────────┬─────────────┬──────────────────────────────────────────────┐
    │ name          │ alias       │ description                                  │
    │ application   │ application │ Generate a new application workspace         │
    │ class         │ cl          │ Generate a new class                         │
    │ configuration │ config      │ Generate a CLI configuration file            │
    │ controller    │ co          │ Generate a controller declaration            │
    │ decorator     │ d           │ Generate a custom decorator                  │
    │ filter        │ f           │ Generate a filter declaration                │
    │ gateway       │ ga          │ Generate a gateway declaration               │
    │ guard         │ gu          │ Generate a guard declaration                 │
    │ interceptor   │ itc         │ Generate an interceptor declaration          │
    │ interface     │ itf         │ Generate an interface                        │
    │ library       │ lib         │ Generate a new library within a monorepo     │
    │ middleware    │ mi          │ Generate a middleware declaration            │
    │ module        │ mo          │ Generate a module declaration                │
    │ pipe          │ pi          │ Generate a pipe declaration                  │
    │ provider      │ pr          │ Generate a provider declaration              │
    │ resolver      │ r           │ Generate a GraphQL resolver declaration      │
    │ resource      │ res         │ Generate a new CRUD resource                 │
    │ service       │ s           │ Generate a service declaration               │
    │ sub-app       │ app         │ Generate a new application within a monorepo │
    └───────────────┴─────────────┴──────────────────────────────────────────────┘

Options:
  -d, --dry-run                      Report actions that would be taken without writing out results.
  -p, --project [project]            Project in which to generate files.
  --flat                             Enforce flat structure of generated element.
  --no-flat                          Enforce that directories are generated.
  --spec                             Enforce spec files generation. (default: true)
  --skip-import                      Skip importing (default: false)
  --no-spec                          Disable spec files generation.
  -c, --collection [collectionName]  Schematics collection to use.
  -h, --help                         Output usage information.
Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
  • I've just attached a screenshot with my question, could you please have a look on it ! That is what I was trying to do and i got error instead. – Alfha Mar 26 '23 at 20:37
  • @Alfha I've added the output of `nest g --help` which shows available schematics that are native to Nest. Where are you getting the information that `route` is a valid schematic to use? That's not anywhere in the [docs](https://docs.nestjs.com/cli/usages#nest-generate) – Jay McDoniel Mar 26 '23 at 21:08
  • Now i got it, tutorial has typo, there should be ```nest generate controller cars``` thank you for your help. – Alfha Mar 27 '23 at 04:19
0

I had to use end of command options, ie -- to make this work

npx nest g gu --no-flat --  guards/my-guard

So to generalise, the below may work:

npx nest g [schematic] --no-flat --  [resource-name]

note: contrary to your scenario, the schematic guard or its short form gu did exist in my case. Just that NestJS couldn't find it for some reason.

sjsam
  • 21,411
  • 5
  • 55
  • 102