Circular dependency in Nest JS
Tags: webdev, javascript, node, programming
Published: 2023-03-02 13:13:51

> I recently came about this Error while running a nest js application


![Nest Error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mk5cbp7653u99eyrrtlc.png)

Firstly, it is important to know that what brought about circular dependency is Dependency Injection
So to have an understanding of circular dependency, one needs to understand dependency injection

[**Dependency injection**](https://www.techtarget.com/searchapparchitecture/definition/dependency-injection) is the process of supplying a resource that a given piece of code requires. The required resource, which is often a component of the application itself is called a **dependency**. 

Basically, instead of having a class declare objects that it depends on, these objects should be fed to your class.

This essentially means our code is more decoupled and testable.

Even though dependency injection makes code easier to write and test, we occasionally encounter some issues one of which is **circular dependency**



let's picture this; 
Say we have two services; **A** and **B**.

If service **A**, depends on a resource in service **B** and service **B** depends on a resource in **A** to run, then we have a circular dependency.

To compile **A**, you must first Compile **B**, but you can't do that because **A** requires **B** to be compiled.

circular dependencies can arise in Nest between modules and between providers.

> As much as possible, circular dependencies should be avoided, but in cases where you can't, nest provides a workaround


A forward reference allows Nest to reference classes that aren't yet defined using the `forwardRef()` utility function. This can be imported from the `@nestjs/common package`


let's look at some code, shall we?

**user.js**
```
@injectable()
export class UserService {
constructor(
@inject(forwardRef(()=> BlogService))
private blogService: BlogService

```
**blog.js**
```
@injectable()
export class BlogService {
constructor(
@inject(forwardRef(()=> UserService))
private userService: UserService

```
from the above, we can see that both User service and Blog service need each other hence a `forwardRef()`

This can also be done on the module level on both sides of the association as well

**user.module.js**

```

@Module({
imports: [forwardRef(() => BlogModule)]})
export class UserModule {}

```

**

## Removing the dependency
**
One major problem with circular dependency is that it creates [tight couplings](https://nordicapis.com/the-difference-between-tight-coupling-and-loose-coupling/) which the famous [SOLID principles](https://www.freecodecamp.org/news/solid-principles-explained-in-plain-english/) preach against.

In the above example, Circular Dependency can be removed by extracting the common features of both services into a new service that would simply depend on the services.
 
In our case, a third service called **profile service** is introduced to hold the necessary information relevant to both the user and blog service. This eventually breaks the cycle

_**With circular dependency**_
``UserService -> BlogService
BlogService -> UserService
``

**_Without circular dependency_**
`UserService -> ProfileService
BlogService -> ProfileService`




I would love to know if you've encountered circular dependency in nest js and how you resolved it 😄.
Kindly share your thoughts in the comments below!

Thanks