You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/cli/usages.md
+9-13Lines changed: 9 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,6 +34,7 @@ Creates and initializes a new Nest project. Prompts for package manager.
34
34
|`--package-manager [package-manager]`| Specify package manager. Use `npm`, `yarn`, or `pnpm`. Package manager must be installed globally.<br/> Alias: `-p`|
35
35
|`--language [language]`| Specify programming language (`TS` or `JS`).<br/> Alias: `-l`|
36
36
|`--collection [collectionName]`| Specify schematics collection. Use package name of installed npm package containing schematic.<br/> Alias: `-c`|
37
+
|`--strict`| Start the project with the following TypeScript compiler flags enabled: `strictNullChecks`, `noImplicitAny`, `strictBindCallApply`, `forceConsistentCasingInFileNames`, `noFallthroughCasesInSwitch`|
37
38
38
39
#### nest generate
39
40
@@ -63,8 +64,8 @@ $ nest g <schematic> <name> [options]
63
64
|`filter`|`f`| Generate a filter declaration. |
64
65
|`gateway`|`ga`| Generate a gateway declaration. |
65
66
|`guard`|`gu`| Generate a guard declaration. |
66
-
|`interface`|| Generate an interface. |
67
-
|`interceptor`|`in`| Generate an interceptor declaration. |
67
+
|`interface`|`itf`| Generate an interface. |
68
+
|`interceptor`|`itc`| Generate an interceptor declaration. |
68
69
|`middleware`|`mi`| Generate a middleware declaration. |
69
70
|`module`|`mo`| Generate a module declaration. |
70
71
|`pipe`|`pi`| Generate a pipe declaration. |
@@ -88,6 +89,12 @@ $ nest g <schematic> <name> [options]
88
89
89
90
Compiles an application or workspace into an output folder.
90
91
92
+
Also, the `build` command is responsible for:
93
+
94
+
- mapping paths (if using path aliases) via `tsconfig-paths`
95
+
- annotating DTOs with OpenAPI decorators (if `@nestjs/swagger` CLI plugin is enabled)
96
+
- annotating DTOs with GraphQL decorators (if `@nestjs/graphql` CLI plugin is enabled)
97
+
91
98
```bash
92
99
$ nest build <name> [options]
93
100
```
@@ -152,17 +159,6 @@ $ nest add <name> [options]
152
159
| -------- | ---------------------------------- |
153
160
|`<name>`| The name of the library to import. |
154
161
155
-
#### nest update
156
-
157
-
Updates `@nestjs` dependencies in the `package.json``"dependencies"` list to their `@latest` version.
|`spec`| boolean _or_ object | If the value is boolean, a value of `true` enables `spec` generation by default and a value of `false` disables it. A flag passed on the CLI command line overrides this setting, as does a project-specific `generateOptions` setting (more below). If the value is an object, each key represents a schematic name, and the boolean value determines whether the default spec generation is enabled / disabled for that specific schematic. |
202
+
|`flat`| boolean | If true, all generate commands will generate a flat structure |
202
203
203
204
The following example uses a boolean value to specify that spec file generation should be disabled by default for all projects:
204
205
@@ -211,6 +212,17 @@ The following example uses a boolean value to specify that spec file generation
211
212
}
212
213
```
213
214
215
+
The following example uses a boolean value to specify flat file generation should be the default for all projects:
216
+
217
+
```javascript
218
+
{
219
+
"generateOptions": {
220
+
"flat":true
221
+
},
222
+
...
223
+
}
224
+
```
225
+
214
226
In the following example, `spec` file generation is disabled only for `service` schematics (e.g., `nest generate service...`):
Copy file name to clipboardExpand all lines: content/exception-filters.md
+27-3Lines changed: 27 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -60,16 +60,24 @@ in the `response` argument. To override the entire JSON response body, pass an o
60
60
The second constructor argument - `status` - should be a valid HTTP status code.
61
61
Best practice is to use the `HttpStatus` enum imported from `@nestjs/common`.
62
62
63
-
Here's an example overriding the entire response body:
63
+
There is a **third** constructor argument (optional) - `options` - that can be used to provide an error [cause](https://nodejs.org/en/blog/release/v16.9.0/#error-cause). This `cause` object is not serialized into the response object, but it can be useful for logging purposes, providing valuable information about the inner error that caused the `HttpException` to be thrown.
64
+
65
+
Here's an example overriding the entire response body and providing an error cause:
64
66
65
67
```typescript
66
68
@@filename(cats.controller)
67
69
@Get()
68
70
asyncfindAll() {
69
-
thrownewHttpException({
71
+
try {
72
+
awaitthis.service.findAll()
73
+
} catch (error) {
74
+
thrownewHttpException({
70
75
status: HttpStatus.FORBIDDEN,
71
76
error: 'This is a custom message',
72
-
}, HttpStatus.FORBIDDEN);
77
+
}, HttpStatus.FORBIDDEN, {
78
+
cause: error
79
+
});
80
+
}
73
81
}
74
82
```
75
83
@@ -130,6 +138,22 @@ Nest provides a set of standard exceptions that inherit from the base `HttpExcep
130
138
-`GatewayTimeoutException`
131
139
-`PreconditionFailedException`
132
140
141
+
All the built-in exceptions can also provide both an error `cause` and an error description using the `options` parameter:
Using the above, this is how the response would look:
148
+
149
+
```json
150
+
{
151
+
"message": "Something bad happened",
152
+
"error": "Some error description",
153
+
"statusCode": 400,
154
+
}
155
+
```
156
+
133
157
#### Exception filters
134
158
135
159
While the base (built-in) exception filter can automatically handle many cases for you, you may want **full control** over the exceptions layer. For example, you may want to add logging or use a different JSON schema based on some dynamic factors. **Exception filters** are designed for exactly this purpose. They let you control the exact flow of control and the content of the response sent back to the client.
Copy file name to clipboardExpand all lines: content/faq/errors.md
+36-4Lines changed: 36 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,17 +17,49 @@ Potential solutions:
17
17
})
18
18
```
19
19
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).
21
21
22
22
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.
23
23
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.
25
25
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 in NestJS. 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`.
27
59
28
60
#### "Circular dependency" error
29
61
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:
@@ -70,7 +71,10 @@ By default a hybrid application will not inherit global pipes, interceptors, gua
70
71
To inherit these configuration properties from the main application, set the `inheritAppConfig` property in the second argument (an optional options object) of the `connectMicroservice()` call, as follow:
0 commit comments