Skip to content

Commit 830e839

Browse files
Merge pull request #2152 from micalevisk/master
docs(faq,techniques): add resolution for "can't resolve ModuleRef"; add missing docs on `axiosRef`
2 parents e550b67 + 2f2b3cd commit 830e839

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

content/faq/errors.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,49 @@ Potential solutions:
1717
})
1818
```
1919
20-
The most common culprit of the error, is not having the `provider` in the module's `providers` array. Please make sure that the provider is indeed in the `providers` array and following [standard NestJS provider practices](/fundamentals/custom-providers#di-fundamentals).
20+
The most common culprit of the error, is not having the `<provider>` in the module's `providers` array. Please make sure that the provider is indeed in the `providers` array and following [standard NestJS provider practices](/fundamentals/custom-providers#di-fundamentals).
2121

2222
There are a few gotchas, that are common. One is putting a provider in an `imports` array. If this is the case, the error will have the provider's name where `<module>` should be.
2323
24-
If you run across this error while developing, take a look at the module mentioned in the error message and look at its `providers`. For each provider in the `providers` array, make sure the module has access to all of the dependencies. Often times, `providers` are duplicated in a "Feature Module" and a "Root Module" which means Nest will try to instantiate the provider twice. More than likely, the module containing the `provider` being duplicated should be added in the "Root Module"'s `imports` array instead.
24+
If you run across this error while developing, take a look at the module mentioned in the error message and look at its `providers`. For each provider in the `providers` array, make sure the module has access to all of the dependencies. Often times, `providers` are duplicated in a "Feature Module" and a "Root Module" which means Nest will try to instantiate the provider twice. More than likely, the module containing the `<provider>` being duplicated should be added in the "Root Module"'s `imports` array instead.
2525

26-
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.
26+
If the `<unknown_token>` above is the string `dependency`, you might have a circular file import. This is different from the [circular dependency](/faq/common-errors#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.
27+
28+
If the `<unknown_token>` above is the string `Object`, it means that you're injecting using an type/interface without a proper provider's token. To fix that, make sure you're importing the class reference or use a custom token with `@Inject()` decorator. Read the [custom providers page](/fundamentals/custom-providers).
29+
30+
Also, make sure you didn't end up injecting the provider on itself because self-injections are not allowed on NetJS. When this happens, `<unknown_token>` will likely be equal to `<provider>`.
31+
32+
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>`:
33+
34+
```bash
35+
Nest can't resolve dependencies of the <provider> (?).
36+
Please make sure that the argument ModuleRef at index [<index>] is available in the <module> context.
37+
...
38+
```
39+
40+
This likely happens when your project end up loading two Node modules of the package `@nestjs/core`, like this:
41+
42+
```text
43+
.
44+
├── package.json
45+
├── apps
46+
│ └── api
47+
│ └── node_modules
48+
│ └── @nestjs/bull
49+
│ └── node_modules
50+
│ └── @nestjs/core
51+
└── node_modules
52+
├── (other packages)
53+
└── @nestjs/core
54+
```
55+
56+
Solutions:
57+
58+
- For **Yarn** Workspaces, use the [nohoist feature](https://classic.yarnpkg.com/blog/2018/02/15/nohoist) to prevent hoisting the package `@nestjs/core`.
2759
2860
#### "Circular dependency" error
2961
30-
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:
62+
Occasionally you'll find it difficult to avoid [circular dependencies](https://docs.nestjs.com/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:
3163
3264
```bash
3365
Nest cannot create the <module> instance.

content/techniques/http-module.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Next, inject `HttpService` using normal constructor injection.
3232
@@filename()
3333
@Injectable()
3434
export class CatsService {
35-
constructor(private httpService: HttpService) {}
35+
constructor(private readonly httpService: HttpService) {}
3636

3737
findAll(): Observable<AxiosResponse<Cat[]>> {
3838
return this.httpService.get('http://localhost:3000/cats');
@@ -131,3 +131,19 @@ HttpModule.registerAsync({
131131
useExisting: HttpConfigService,
132132
});
133133
```
134+
135+
#### Using Axios directly
136+
137+
If you think that `HttpModule.register`'s options are not enough for you, or if you just want to access the underlying Axios instance created by `@nestjs/axios`, you can access it via `HttpService#axiosRef` as follows:
138+
139+
```typescript
140+
@Injectable()
141+
export class CatsService {
142+
constructor(private readonly httpService: HttpService) {}
143+
144+
findAll(): Promise<AxiosResponse<Cat[]>> {
145+
return this.httpService.axiosRef.get('http://localhost:3000/cats');
146+
// ^ AxiosInstance interface
147+
}
148+
}
149+
```

0 commit comments

Comments
 (0)