ng Verified current stable Not installed? Package Management

Ng / Create New Service

Create New Service

Create a new service in the Angular application.

$
Terminal
ng g s <service_name>

When To Use

To scaffold a service for handling business logic or data retrieval in Angular apps.

Pro Tip

Consider using `--module` option to specify module association from the onset.

Command Builder

Tune the command before you copy it

Back to syntax
$
Generated Command
ng g s <service_name>

Anatomy of Output

Understanding the result

CREATE src/app/auth-service.service.ts File Created

Service file created successfully.

UPDATE src/app/app.module.ts Module Update

Service has been registered in the appropriate module.

Service includes CRUD methods skeleton Methods Skeleton

Basic methods for create, read, update, and delete are scaffolded.

Power User Variants

Optimized versions

ng generate service auth --skip-tests

Generate a service without creating spec files.

ng generate service user --module=users

Link the service directly to a specified module.

Troubleshooting

Common pitfalls

Service not found: x

Solution: Verify that your service name is spelled correctly.

Module not specified, unable to link service

Solution: Use the --module flag to specify the target module.

Folder does not exist: src/app/

Solution: Ensure the project structure is correct and src/app/ exists.

Command Breakdown

What each part is doing

ng
Base Command
The executable that performs this operation. Here it runs Ng before the shell applies any redirect operators.
g
g|generate
The value supplied for g|generate.
s
s|service
The value supplied for s|service.
<service_name>
service name
The value supplied for service name.

Alternative Approaches

Comparable commands in other tools

Alternative package management tools for the same job.