5

I want to create service and register it with app.module, but when I run this command

ng g s bram -m=app.module

I get an error:

Unknown option: "-m"

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kumar Shubham
  • 81
  • 2
  • 6

3 Answers3

3

Generate service doesn’t take a module argument as services don’t really “belong” to a module. To provide in root just open your service and put

@Injectable({
  providedIn: ‘root’
})

Here are the docs for service. As you can see, service does not take a module argument.

E_net4
  • 27,810
  • 13
  • 101
  • 139
bryan60
  • 28,215
  • 4
  • 48
  • 65
  • ungh, this is exactly a thing that puts me off when learning new stuff - when tooling gets updated so rapidly that even the community is not able to follow it. but I guess there is a reason for everything. In the current version of angular that I just installed, the above Injectable decorator is inserted by default, so the equivalent of the old -m app.module syntax is to not use it at all. (see Angular 6 answers) https://stackoverflow.com/questions/42748773/angular-cli-generate-a-service-and-include-the-provider-in-one-step – hello_earth May 09 '20 at 10:43
  • 1
    They deprecated it because module level provided services were confusing and had unclear meaning. Services should be root provided if they’re singletons or component provided if not singletons to be clear about which service you’re injecting . In all angular versions, unless lazy loading, there is no difference between a service provided in a feature module or root module in terms of availability or injection. The duplication of module provided services in lazy loaded modules is much more often a bug than intended behavior, so they try to discourage the practice by removing the cli argument. – bryan60 May 09 '20 at 12:10
2

command triedTry using

ng g s services/bram --module=app.module
Seba Cherian
  • 1,755
  • 6
  • 18
0

The option --module is not available for a service : https://angular.io/cli/generate#service

But its available for a componant : https://angular.io/cli/generate#component