ng generate: Your Project's Code Wizard

ng generate is a command-line wizard for scaffolding new Angular features. It doesn't just create files; it wires them into your app. Use ng g component my-component to create components, services, and more.
Why it exists
To solve the problem of repetitive boilerplate and manual configuration when adding new features to an Angular application. Creating a new component, for example, involves multiple files and updates to module declarations; doing this by hand is tedious and error-prone. ng generate automates this process to enforce consistency and save time.
The mental model
Think of ng generate (or its alias ng g) as an intelligent templating engine for your project. You tell it WHAT you want (a component, a service, a pipe), and it handles the HOW—creating the necessary files, filling them with standard boilerplate, and, crucially, updating other parts of your application to integrate the new piece.
How it works
You run the command with a schematic, like ng generate component my-new-component. The Angular CLI finds the appropriate schematic, which is a set of instructions for code generation. It then creates the files (e.g., .ts, .html, .css, .spec.ts) and modifies related files, such as the parent module, to declare the new component. This system is extensible and can be customized for specific project needs.
When to use it
Use it whenever you're adding a standard piece of an Angular application. This is the default, idiomatic way to create components, services, directives, pipes, guards, interceptors, and modules. It ensures that all developers on a team are creating code structures in the same way.
When not to use it
Avoid it for one-off utility files or code that doesn't fit a standard Angular pattern. If you're creating a simple TypeScript interface or a helper function that isn't a service, just creating the file manually is faster and more appropriate. The power of ng generate is in automating repeated, structured tasks.
One canonical example
To see what ng generate will do without actually changing your project, use the --dry-run flag (alias -d). Running ng g component user-profile --dry-run will list all the files it would create (like src/app/user-profile/user-profile.component.ts) and which files it would update (like src/app/app.module.ts), all without writing to disk. This is the safest way to use the command and avoid surprises.
Interview question
Which of the following best describes a primary advantage of using ng generate for creating a new Angular component?
- a.It creates all required files and automatically declares the component in its associated Angular module.Correct
- b.It provides an interactive graphical interface for designing the component's layout.
- c.It ensures the component is immediately registered in the application's routing configuration.
- d.It automatically optimizes the component's rendering performance.
Why? this is the answer
The core benefit of ng generate is automating boilerplate and integration; it creates necessary files and crucially updates the parent module to declare the new component. While components are often routed, ng generate's direct integration step is module declaration, not routing configuration.
Just read this? Test yourself on what you have been reading.
Read the original → angular.dev
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on angular — each one lists the topics its interview covers.
See open roles