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 3 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.

egghead
egghead
~ 5 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today