Last updated: Apr 4, 2024
Reading time·4 min

To generate a component in a specific directory or a subdirectory:
Open your terminal in your project's root directory (where your
package.json file is).
Run the npx ng generate component path/to/component command.
Here is an example.
npx ng generate component auth/users

As shown in the screenshot, the component is generated at
src/app/auth/users/users.component.ts.
The command created an auth/users/ directory in my app folder.

After generating the component in a subfolder, open your app.module.ts file
and make sure the component is imported correctly.
In my case, the relevant import statement looks as follows.
import { UsersComponent } from './auth/users/users.component'; @NgModule({ // 👇️ Make sure the component is added to your declarations array declarations: [AppComponent, UsersComponent], // ... })

Notice that the path that was generated for the component is
src/app/auth/users/users.component.ts.
If you don't want to create a users folder, issue the command with the
--flat option.
--flat optionThe --flat option creates the new files at the top level of the current
project.
npx ng generate component auth/users --flat

Notice that the users directory is not created when the --flat option is
set.
Instead, the users component is generated in the auth directory.
The path is the following: src/app/auth/users.component.css.

After you generate a component in a specific directory, always make sure to
check your app.module.ts file to verify the component is imported correctly
and added to the declarations array.

When using this approach you can generate other components in the auth/
directory.
npx ng generate component auth/admins --flat

The admins.component.ts file is generated in the auth directory, right next
to the users.component.ts file.

An alternative approach is to open your terminal in the specific directory in which you want to generate a component.
This is quite easy in Visual Studio Code:
Right-click on the directory in which you want to create a component in the left sidebar.
Select Open in Integrated Terminal.

npx ng generate component component-name command.npx ng generate component authors

I issued the command from my src/app/auth directory, so it generated a
component at src/app/auth/authors/authors.component.ts.
Notice that the command generated a separate folder for the component
(authors/).

If you don't want to create a specific folder for the component, use the
--flat option.
Right-click on the directory in which you want to create a component in the left sidebar.
Select Open in Integrated Terminal.

npx ng generate component component-name --flat command.npx ng generate component authors --flat

The component is now generated at src/app/auth/authors.component.ts.
Notice that the authors/ directory wasn't created this time.
The authors component is now generated directly in the auth/ folder.
An alternative way to generate a component in a specific folder is to:

cd command to change to the specific directory.cd the/path/you/copied
npx ng generate component-name command.npx ng generate component component-name

If you want to generate the component at the top level, use the --flat option.
npx ng generate component component-name --flat
Note that the generated components must be in the app/ directory.
I've also written an article on how to open your terminal in the directory of the currently opened file in VS Code.
You can learn more about the related topics by checking out the following tutorials: