---
title: Generating code
description: Write components, services, pipes, guards, libraries, apps and more with ngp-generate, the plugin's stand-in for ng generate, with Vitest specs.
---

# Generating code

You've typed `ng g c user-card` for years, and an app on Vite has no `ng`. Vite has no generators either: `npm create vite` starts a project and does nothing after that. The plugin's package carries a command for the job instead:

```bash
npx ngp-generate component user-card
```

It writes what `ng generate` writes, with the file names and the code of the Angular CLI's own schematics, and specs that run under [Vitest](testing). It needs no `angular.json`, no `@angular/cli` and no `@angular-devkit`. It comes with the plugin, so an app from [`create-ngp`](creating-an-app) has it already.

## Run it

From the app's folder, with the package manager the app uses:

| Package manager | Command |
|---|---|
| npm | `npx ngp-generate c user-card` |
| pnpm | `pnpm exec ngp-generate c user-card` |
| Yarn | `yarn ngp-generate c user-card` |
| bun | `bunx ngp-generate c user-card` |
| [Vite+](vite-plus#the-toolchain-with-angular) | `vpx ngp-generate c user-card` |

It prints each file it wrote, the way `ng generate` does:

```text
CREATE src/app/user-card/user-card.ts (195 bytes)
CREATE src/app/user-card/user-card.html (24 bytes)
CREATE src/app/user-card/user-card.css (0 bytes)
CREATE src/app/user-card/user-card.spec.ts (413 bytes)
```

To use it often, add a script to `package.json`, `"g": "ngp-generate"`, and run `npm run g -- c user-card`.

## What each schematic writes

The names follow the Angular CLI from Angular 20 on: no `.component` or `.service` suffix on the file or the class, and a `-pipe`, `-guard`, `-interceptor` or `-resolver` suffix on the file where the CLI keeps one. The short names are the CLI's.

| Schematic | Short | `ngp-generate <schematic> user` writes |
|---|---|---|
| `component` | `c` | `user/user.ts`, `user.html`, `user.css`, `user.spec.ts`: class `User`, selector `app-user`, in a folder of its own |
| `service` | `s` | `user.ts`, `user.spec.ts`: class `User`, `providedIn: 'root'` |
| `directive` | `d` | `user.ts`, `user.spec.ts`: class `User`, selector `[appUser]` |
| `pipe` | `p` | `user-pipe.ts`, `user-pipe.spec.ts`: class `UserPipe`, pipe name `user` |
| `guard` | `g` | `user-guard.ts`, `user-guard.spec.ts`: `userGuard`, a `CanActivateFn` |
| `interceptor` | | `user-interceptor.ts`, `user-interceptor.spec.ts`: `userInterceptor`, an `HttpInterceptorFn` |
| `resolver` | `r` | `user-resolver.ts`, `user-resolver.spec.ts`: `userResolver`, a `ResolveFn<boolean>` |
| `library` | `lib` | `libs/user/`, in a monorepo only: see [Libraries](generating-code#libraries) |
| `application` | `app` | `apps/user/`, in a monorepo only: see [Applications](generating-code#applications) |

Everything is standalone. Guards, interceptors and resolvers are functions, which is what the CLI writes by default; there's no class-based variant.

The name can be in any case, `UserCard`, `userCard` or `user-card`, and the files come out dasherized. It can carry folders: `ngp-generate c admin/user-card` writes `src/app/admin/user-card/user-card.ts`.

## Where the files go

Under the project's `src/app`. A project with no `src/app` but a `src/lib`, like a library, gets them under `src/lib`, with the selector prefix `lib` instead of `app`; with neither, under `src`. The project is the folder you run the command in, or the one `--project` names. Nothing reads a workspace file: the folder is the project.

It never overwrites a file. If any file it would write exists, it writes none of them and says which.

## Libraries

In a monorepo from [`create-ngp --monorepo`](creating-an-app#the-monorepo-layout), `ngp-generate library cart` adds a library, from wherever you run it in the monorepo:

```text
CREATE libs/cart/tsconfig.json (118 bytes)
CREATE libs/cart/vitest.config.ts (535 bytes)
CREATE libs/cart/src/index.ts (33 bytes)
CREATE libs/cart/src/test-setup.ts (661 bytes)
CREATE libs/cart/src/lib/cart/cart.ts (176 bytes)
CREATE libs/cart/src/lib/cart/cart.html (19 bytes)
CREATE libs/cart/src/lib/cart/cart.css (0 bytes)
CREATE libs/cart/src/lib/cart/cart.spec.ts (392 bytes)
UPDATE tsconfig.base.json (732 bytes)
```

That's a component, `Cart` with the selector `lib-cart`, exported from `src/index.ts`, a Vitest project, and one line in `tsconfig.base.json` that maps `@shop/cart` to `./libs/cart/src/index.ts`. The scope comes from the name in the top `package.json`: `shop` gives `@shop`, and `@acme/shop` gives `@acme`. `--import-path` names it yourself. An app imports it as `import { Cart } from '@shop/cart'`, and no build step comes between: the app compiles the library's files as it compiles its own.

The monorepo is the folder with `tsconfig.base.json`, found from where you run the command upwards. Without one, `ngp-generate library` stops and says so. A library goes straight under `libs/`, so its name can't carry folders. Its `tsconfig.base.json` is rewritten as JSON with two-space indents, which drops any comment in it, so it refuses a file with comments rather than lose them.

Components and services go into a library with `--project`: `ngp-generate c cart-item --project libs/cart` writes `libs/cart/src/lib/cart-item/`, with the `lib` prefix, and adds `export * from './lib/cart-item/cart-item';` to `libs/cart/src/index.ts`, so apps can import it straight away. That holds for every schematic in a project with `src/lib` and `src/index.ts`. `--no-export` leaves the index alone, for code the library keeps to itself.

## Applications

`ngp-generate application admin` adds a second app to the monorepo, in `apps/admin`:

- **The files of `apps/web`**, as `create-ngp` writes them for the `app` template, with the page title `admin` and the single app's home page, which doesn't import `libs/ui`. They're copied from create-ngp's template when the plugin is built, and a test fails when the two differ.
- **Three scripts** in the top `package.json`: `dev:admin`, `build:admin` and `preview:admin`, running `vite dev apps/admin` and the rest. After `vp migrate` they run `vp` instead, matching the scripts it rewrote. `npm test` already runs the new app's spec, through the top `vitest.config.ts`.

The app is browser-only. There's no server-rendered variant: for one, copy `apps/web` from an `app-ssr` monorepo. Two apps' dev servers can run at once; the second takes the next free port.

## Routes

`--route <path>` makes a component reachable: `ngp-generate c orders --route orders` writes the component and adds a lazy route to `src/app/app.routes.ts`:

```ts
{ path: 'orders', loadComponent: () => import('./orders/orders').then((m) => m.Orders) },
```

It goes at the end of the `routes` array, which it finds by its declaration, `export const routes: Routes = [`, as `ng new` writes it. A routes file shaped any other way, a path that's already there, or a schematic other than `component` stops it before it writes anything.

## Options

| Option | What it does |
|---|---|
| `--project <dir>` | The project's folder. Default: the folder you're in |
| `--flat`, `--no-flat` | No folder of its own, or one. Default: a folder for a component, none for everything else, as in the CLI |
| `--skip-tests` | No `.spec.ts` |
| `-t, --inline-template` | The component's template in its `.ts` file |
| `-s, --inline-style` | The component's styles in its `.ts` file |
| `--style <ext>` | `css`, `scss`, `sass`, `less` or `none`. Default: the extension of `src/styles.*`, so an app whose global stylesheet is `styles.scss` gets `.scss` components; else `css`. Sass and Less need their compiler installed, as [Component styles](styles) says |
| `-p, --prefix <prefix>` | The selector prefix. Default `app`, or `lib` under `src/lib` |
| `--implements <type>` | The guard's type: `CanActivate` (the default), `CanActivateChild`, `CanDeactivate` or `CanMatch` |
| `--import-path <path>` | What apps import a library by. Default `@<scope>/<name>` |
| `--route <path>` | Add a lazy route to the component in `src/app/app.routes.ts`. See [Routes](generating-code#routes) |
| `--no-export` | In a library, don't add the new file to `src/index.ts` |
| `--dry-run` | List what it would write, and write nothing |
| `-h, --help` | Print the schematics and the options |

## How it differs from ng generate

- **The specs import from Vitest.** Each spec imports `describe`, `it` and `expect` from `vitest`, as the templates' spec does, because Vitest's globals are off by default. A component spec waits with `await fixture.whenStable()` instead of calling `fixture.detectChanges()`, which suits a zoneless app. Under Vite+, where `vp migrate` rewrote your existing specs to import `vite-plus/test`, a generated spec still imports `vitest`, and `vp test` runs it as it is.
- **Few edits to existing files.** `ng generate component` in an NgModule app adds the component to the nearest module. Standalone components need no registration, so `ngp-generate` writes new files and edits only three, each when it's the point: `app.routes.ts` with `--route`, a library's `src/index.ts`, and for `library` and `application` the top `tsconfig.base.json` and `package.json`. It never touches `app.config.ts`.
- **No configuration file.** `ng generate` reads defaults from `angular.json`'s `schematics` block. `ngp-generate` reads only `src/styles.*`, for `--style`, and for a library the top `package.json`'s name and `tsconfig.base.json`. Pass anything else as a flag.
- **Not the CLI's whole list.** There's no `class`, `enum`, `interface`, `module`, `web-worker` or `environments`. An NgModule (`ng g m`) is the one to miss most if your app still has them. `library` exists, but writes a library that compiles with the app, not one ng-packagr builds and you publish, which is what `ng g library` sets up.

## Where this is weak

- **It's a copy of Angular's templates, and copies drift.** The output follows the schematics of `@schematics/angular` 21.0.0-next.3, the release read when the command was written. When Angular changes what `ng generate` writes, as Angular 20 did with the file names, `ngp-generate` keeps writing the old shape until someone updates it. A test generates one of each schematic into a fresh app, then builds it with strict templates and runs the eight specs, as a Vite project and under Vite+, and another adds a library to a monorepo and runs its spec. So it catches output that stops compiling, but not output that's merely out of style.
- **Formatting is yours.** The files use single quotes and two-space indents, like the templates. If your project runs Prettier or Oxfmt with other settings, run it after generating. Under Vite+, `vp check --fix` does that.
- **No Angular CLI schematics from libraries.** `ng add` and `ng generate @angular/material:table`, schematics that a library ships, need the Angular CLI's schematics engine, which this isn't. They haven't been tried by running `@angular-devkit/schematics-cli` against an app without `angular.json`, and most of them read that file.
