How do you generate services etc. for a @nrwl/nx angular-cli app?
Asked Answered
P

4

7

Normally when using @angular/cli to develop an application, you add a new service in the following manner:

ng generate service foo

This would generate "FooService".

My question is, when you have generated a library using @nrwl/nx, how do you add a service to it?

ng generate lib foo-lib

What I want to know, is the command to generate service "foo", so that it lives in the foo-lib library.

Pilloff answered 27/11, 2017 at 23:11 Comment(0)
G
7

It just works with the angular-cli command, but you have to add the --app flag

ng g service foo --app=foo-lib

It is also mentioned in the docs of Nx under "Leverage the AngularCLI".

Gyro answered 6/12, 2017 at 7:30 Comment(1)
In recent versions, Nx uses project.json, instead of angular.json for project configs, so there's no ng workspace and the above command won't work. Therefore @Andrew's is the only working answer.Kyanize
C
20

For nx vesion 6.1, this is the right way:

ng g service my-service --project=myNameOrLibName
Clinic answered 17/6, 2018 at 8:40 Comment(0)
G
7

It just works with the angular-cli command, but you have to add the --app flag

ng g service foo --app=foo-lib

It is also mentioned in the docs of Nx under "Leverage the AngularCLI".

Gyro answered 6/12, 2017 at 7:30 Comment(1)
In recent versions, Nx uses project.json, instead of angular.json for project configs, so there's no ng workspace and the above command won't work. Therefore @Andrew's is the only working answer.Kyanize
M
6

I'm using version 15.7.2 of Nx. I couldn't find anything in the online documentation that discussed how to create an angular service, which I believe is a fault in the Nx docs. Then, I tried the following and was able to create a service.

Here's the command I used:

npx nx g @nrwl/angular:service services/my-service --project=my-project
Miscalculate answered 28/2, 2023 at 7:34 Comment(3)
The docs clearly describes it here: nx.dev/nx-api/angular#fallback-to : If you try to invoke a generator that is not present in @nx/angular, the request will automatically be forwarded on to @schematics/angular, So, even though there is no @nx/angular:service generator, the following command will successfully create a service: nx g @nx/angular:service my-serviceKyanize
@Kyanize On Feb 28th, I let the Nx people know about the lack of documentation by filing a bug. On Mar 3rd, they updated the documentation to make explicit this fallback behavior. So, of course it "clearly describes" the behavior thanks to me. Here is the edit to the docs including date of change.Miscalculate
In version 16.10.0 I ran it like this: npx nx g @nx/angular:service ./services/my-serviceForbes
C
1

Nx is a wrapper around angular cli. Everything you do using Angular-cli, you pretty much do in nx workspaces. Only difference being the custom schematic that nx provides that helps you achieve mono-repo and re-use code across multiple apps within your company. When in doubt, just run any commands with --dry-run and see if things being generated are the things you want and you should be good.

Caylor answered 10/1, 2018 at 6:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.