Closed emochka2007 closed 5 months ago
This is a very similar issue to this one https://github.com/nestjs/nest/issues/13441#issuecomment-2058334460
The root cause is exactly the same - duplicated @nestjs
packages (local copies installed for packages and apps)
This could be better discussed on our Discord. If you haven't already, please join here and send a new post in the # 🐈 nestjs-help forum. Make sure to include a link to this issue, so you don't need to write it all again. We have a large community of helpful members, who will assist you in getting this to work.
Is there an existing issue for this?
Current behavior
Problems Encountered with NestJS and pnpm in a Monorepo Setup
Overview
I've been working with NestJS in conjunction with pnpm for managing a monorepo workspace and have encountered numerous issues. Below is the structure of my
pnpm-workspace.yaml
and a description of the shared folders and microservices within the workspace.Workspace Configuration
My
pnpm-workspace.yaml
is configured as follows:yaml
The packages directory contains a shared folder, and the apps directory is home to my microservices.
Encountered Issues
TypeORM Synchronization Error While using TypeORM, I frequently get this error upon rebuilding the project: Interestingly, this occurs when synchronize: true is set in the configuration.
exception EntityMetadataNotFoundError: No metadata for "User" was found.
NestJS Dependency Resolution Error
Another recurring error is related to dependency resolution in NestJS:
Nest can't resolve dependencies of the TypeormModule.
The workaround involves rebuilding the entire project and deleting the root node_modules folder.Custom Interceptor Dependency Issue
Implementing a custom interceptor results in the following dependency error:
ERROR [ExceptionHandler] Nest can't resolve dependencies of the CustomCacheInterceptor (CACHE_MANAGER, ?). Please make sure that the argument Reflector at index [1] is available in the PostModule context.
Guard Reflection Error
Using custom guards yields a 'not found' error for the reflector: However, this is fixed by passing a new instance of Reflector to the guard.
ERROR [ExceptionHandler] Nest can't resolve dependencies of the CustomGuard (?). Please make sure that the argument Reflector at index [0] is available in the context.
TypeScript Typing Mismatch
Consistent TypeScript errors are thrown regarding a type mismatch for RmqClient from @nestjs/microservices, indicating an incompatibility between the imported type and the type declared in the shared folder. Attached screenshot illustrates the TypeScript error in more detail.
Example from a microservice gateway:
Minimum reproduction code
https://github.com/emochka2007/nestjs-pnpm-issue.git
Steps to reproduce
Create a Monorepo Workspace:
pnpm
.pnpm-workspace.yaml
to include bothpackages/*
for shared libraries andapps/*
for individual microservices.Set Up TypeORM:
synchronize: true
configuration.Experience the TypeORM Synchronization Error:
EntityMetadataNotFoundError
when the project attempts to rebuild.NestJS Dependency Resolution Error:
node_modules
folder and rebuilding the project.Implement a Custom Interceptor:
@UseInterceptors()
.Custom Interceptor Dependency Issue:
CACHE_MANAGER
andReflector
.Use Guards with Custom Reflector:
@UseGuards()
.Reflector
to the guard.Expected behavior
TypeORM Synchronization:
synchronize: true
configuration in TypeORM should not lead toEntityMetadataNotFoundError
during the project rebuild. Instead, it should automatically synchronize the database schema with the entities defined in the project without errors.NestJS Module Resolution:
node_modules
directory and rebuild the entire project. The resolution should work correctly with the established structure of the monorepo.Custom Interceptors:
CACHE_MANAGER
andReflector
dependencies should be injectable into the interceptors without issue.Guards and Reflectors:
Reflector
should be resolved without needing to manually pass a new instance. The framework should automatically provide the required instances wherever they are needed.Package
Other package
@nestjs/typeorm
NestJS version
^10.3.7
Packages versions
Node.js version
v20.10.0
In which operating systems have you tested?
Other