Skip to content

Commit ff76b4d

Browse files
committed
feat: add resolution for "ModuleRef" dep not found error
1 parent bab28cc commit ff76b4d

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

content/faq/errors.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ During your development with NestJS, you may encounter various errors as you lea
66

77
Probably the most common error message is about Nest not being able to resolve dependencies of a provider. The error message usually looks something like this:
88

9-
```bash
9+
```
1010
Nest can't resolve dependencies of the <provider> (?). Please make sure that the argument <unknown_token> at index [<index>] is available in the <module> context.
1111
1212
Potential solutions:
@@ -25,6 +25,60 @@ If you run across this error while developing, take a look at the module mention
2525

2626
If the `unknown_token` above is the string `dependency`, you might have a circular file import. This is different from the [circular dependency](./errors.md#circular-dependency-error) below because instead of having providers depend on each other in their constructors, it just means that two files end up importing each other. A common case would be a module file declaring a token and importing a provider, and the provider import the token constant from the module file. If you are using barrel files, ensure that your barrel imports do not end up creating these circular imports as well.
2727

28+
##### Error on monorepos
29+
30+
If you are in a monorepo setup, you may face the same error as above but for core provider called `ModuleRef` as a `<unknown_token>`:
31+
32+
```
33+
Nest can't resolve dependencies of the <provider> (?). Please make sure that the argument ModuleRef at index [<index>] is available in the <module> context.
34+
...
35+
```
36+
37+
This likely happens when your project end up loading two Node modules of the package `@nestjs/core`
38+
39+
```
40+
.
41+
├── package.json
42+
├── apps
43+
│ └── api
44+
│ └── node_modules
45+
│ └── @nestjs/bull
46+
│ └── node_modules
47+
│ └── @nestjs/core
48+
└── node_modules
49+
├── (other packages)
50+
└── @nestjs/core
51+
```
52+
53+
###### Fix for Yarn Workspaces
54+
55+
To circumvent that file structure in **Yarn** Workspaces, you can levearage on its [_nohoist_ feature](https://classic.yarnpkg.com/blog/2018/02/15/nohoist):
56+
57+
```json
58+
{
59+
"workspaces": {
60+
"packages": [],
61+
"nohoist": [
62+
"**/@nestjs/**"
63+
]
64+
}
65+
}
66+
```
67+
68+
Then, after `yarn install`, you'll get something like this:
69+
70+
```
71+
.
72+
├── package.json
73+
├── apps
74+
│ └── api
75+
│ └── node_modules
76+
│ ├── @nestjs/core
77+
│ └── @nestjs/bull
78+
└── node_modules
79+
└── (other packages)
80+
```
81+
2882
#### "Circular dependency" error
2983

3084
Occasionally you'll find it difficult to avoid [circular dependencies](/fundamentals/circular-dependency) in your application. You'll need to take some steps to help Nest resolve these. Errors that arise from circular dependencies look like this:

0 commit comments

Comments
 (0)