Questions tagged [nestjs]

Nest (NestJS) is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with and fully supports TypeScript.

Nest is a framework for building efficient, scalable Node.js web applications. It uses modern JavaScript, is built with TypeScript, and combines elements of Object-oriented programming, Functional Programming, and Functional Reactive Programming.

Under the hood, Nest makes use of Express, but also provides compatibility with a wide range of other libraries (e.g. Fastify). This allows for easy use of the myriad third-party plugins which are available.

Nest Resources

11803 questions
141
votes
9 answers

Inject nestjs service from another module

I've got a PlayersModule and an ItemsModule. I want to use the ItemsService in the PlayersService. When I add it by injection: import { Injectable } from '@nestjs/common'; import { InjectModel } from 'nestjs-typegoose'; import { ModelType, Ref }…
Fairydhwen
  • 1,595
  • 3
  • 11
  • 10
126
votes
28 answers

TypeORM Entity in NESTJS - Cannot use import statement outside a module

Started new project with 'nest new' command. Works fine until I add entity file to it. Got following error: import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; ^^^^^^ SyntaxError: Cannot use import statement outside a module What do…
Anton
  • 1,898
  • 3
  • 18
  • 27
123
votes
4 answers

What's the difference between Interceptor vs Middleware vs Filter in Nest.js?

What's the difference between an Interceptor, Filter and Middleware in Nest.js framework? When should one of them be used and favored over the other? Thanks
Bill
  • 2,026
  • 9
  • 55
  • 99
95
votes
1 answer

nestjs vs plain express performance

I've just tested performance on a simple nest's controller, that returns text on a get request (no database). And the same simple GET controller (middleware) with express. I used WRK tool to test performance. And as a result plain express is 2 x…
Shadowfax
  • 1,567
  • 2
  • 13
  • 16
89
votes
5 answers

How to use query parameters in Nest.js?

I am a freshman in Nest.js. And my code as below @Get('findByFilter/:params') async findByFilter(@Query() query): Promise { } I have used postman to test this…
Eve-Sama
  • 2,176
  • 4
  • 20
  • 32
87
votes
6 answers

NestJS - How to use .env variables in main app module file for database connection

I am working on my first NestJS application, which was working fine with hardcoded database connecting string in app.module.ts. But then as per our requirements, I had to pick the database config values from environment files. For that, I followed…
Mohit Bhardwaj
  • 9,650
  • 3
  • 37
  • 64
83
votes
2 answers

What's the difference between tsc (TypeScript compiler) and ts-node?

I'm very confused about the difference between tsc and ts-node. I'm learning TypeScript and I usually transpile server .ts files with tsc command. Now, I'm approaching nestjs framework, and I see that it uses ts-node. So what's the difference…
gremo
  • 47,186
  • 75
  • 257
  • 421
79
votes
18 answers

NestJs: Why do we need DTOs and interfaces both in NestJS

The NestJS documentation showcases how to add DTOs to use in Controllers to validate request objects by using class-validator package. DTOs described there are TypeScript classes. Now, while controllers deal with DTOs(TS Classes), NestJS providers…
Ajitabh
  • 791
  • 1
  • 5
  • 4
78
votes
5 answers

What is the nestjs error handling approach (business logic error vs. http error)?

While using NestJS to create API's I was wondering which is the best way to handle errors/exception. I have found two different approaches : Have individual services and validation pipes throw new Error(), have the controller catch them and then…
Aaron Ullal
  • 4,855
  • 8
  • 35
  • 63
69
votes
7 answers

How to use nestjs Logging service

I tried to use the internal Logger of nestjs (described on https://docs.nestjs.com/techniques/logger -> but with no description of how to use it) But I had problems (tried to inject LoggerService and so on) Can anybody explain how to do this?
maku_at
  • 1,579
  • 2
  • 13
  • 21
68
votes
6 answers

Class-validator - validate array of objects

I am using class-validator package with NestJS and I am looking to validate an array of objects that need to have exactly 2 objects with the same layout: So far I have: import { IsString, IsNumber } from 'class-validator'; export class AuthParam { …
bensiu
  • 24,660
  • 56
  • 77
  • 117
66
votes
12 answers

Environment variable with dotenv and TypeScript

I create this .env file: TYPE=xxx HOST=xxx, PORT=xxx, USERNAME=xxx, PASSWORD=xxx, DATABASE=xxx, in my file I use in this way: import * as dotenv from "dotenv"; dotenv.config(); export const typeOrmConfig: TypeOrmModuleOptions = { port:…
user13111868
66
votes
8 answers

Nest.js - request entity too large PayloadTooLargeError: request entity too large

I'm trying to save a JSON into a Nest.js server but the server crash when I try to do it, and this is the issue that I'm seeing on the console.log: [Nest] 1976 - 2018-10-12 09:52:04 [ExceptionsHandler] request entity too large…
Alexisvt
  • 1,561
  • 1
  • 10
  • 9
63
votes
8 answers

Inject TypeORM repository into NestJS service for mock data testing

There's a longish discussion about how to do this in this issue. I've experimented with a number of the proposed solutions but I'm not having much luck. Could anyone provide a concrete example of how to test a service with an injected repository and…
nurikabe
  • 3,802
  • 2
  • 31
  • 39
63
votes
6 answers

How to implement pagination in NestJS with TypeORM

Is there any way to get the total count and records with a single query, instead of running it as 2 separate queries? If it's not possible, is there any way to reuse the where condition in both queries? async findAll(query): Promise { …
HanJeaHwan
  • 956
  • 1
  • 8
  • 13
1
2 3
99 100