Create a library for a NestJS API in a Nx Workspace

Bram Borggreve
InstructorBram Borggreve
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

In this lesson we use the @nrwl/nest plugin to create a new library called core.

This is done by running the lib schematic from the @nrwl/nest plugin.

Once this library is generated, we import the it in the AppModule in order to register it.

If we look at the server output, we see that the CoreModule can not be loaded, failing with an error message "Module not found".

This is because changes in tsconfig are not being read until we restart the server. After restarting the server our CoreModule gets loaded correctly.

Bram Borggreve: [0:00] In our nx Workspace, we run nx g @nrwl/nest:lib, and pass in the name of the library we want to create. In our case, core.

[0:09] We see that directory core gets created under libs, and workspace.json and nx.json are updated. Additionally, we see that tsconfig got changed. Inside the tsconfig, a new property path got created.

[0:26] Here, we define an alias @coursus/core that points to the index.ts.

[0:31] Everything we export from .index.ts is available under the namespace @coursus/core. When we open apps/api/src/app/app.module.ts, we can import { CoreModule } from '@coursus/core'.

[0:47] When we take a look at the API, we see that there's an error. "Can't resolve '@coursus/core'." This is because the server needs to restart when we make a change in tsconfig. When we restart the server, we see that now it starts and that the CoreModule dependencies are initialized.