NestJS跨仓库模块依赖解析失败问题及解决(npm link相关)
NestJS私有模块跨仓库依赖解析错误及解决方法
问题场景
构建用于私有仓库复用的NestJS模块,经nest build构建后通过npm link关联到主项目时,出现TypeOrm依赖解析错误。模块放在主项目内部时可正常运行,跨仓库引入则触发报错。
错误信息
首次报错:
ERROR [ExceptionHandler] Nest can't resolve dependencies of the UserRepository (?). Please make sure that the argument DataSource at index [0] is available in the TypeOrmModule context. Potential solutions: - Is TypeOrmModule a valid NestJS module? - If DataSource is a provider, is it part of the current TypeOrmModule? - If DataSource is exported from a separate @Module, is that module imported within TypeOrmModule? @Module({ imports: [ /* the Module containing DataSource */ ] })
尝试在AppModule的providers中添加UsersService后,出现新错误:
ERROR [ExceptionHandler] Nest can't resolve dependencies of the UsersService (?). Please make sure that the argument UserRepository at index [0] is available in the AppModule context. Potential solutions: - Is AppModule a valid NestJS module? - If UserRepository is a provider, is it part of the current AppModule? - If UserRepository is exported from a separate @Module, is that module imported within AppModule? @Module({ imports: [ /* the Module containing UserRepository */ ] })
解决方案
问题根源是npm link导致数据库连接无法在主项目与私有模块间共享,改用yalc进行包关联后,问题解决。
相关代码
AppModule
import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { UsersModule, User } from '@MY-PACKAGE-LINKED/user'; @Module({ imports: [ TypeOrmModule.forRootAsync({ useFactory: () => ({ type: 'mysql', host: 'localhost', port: 3306, username: 'root', password: 'root', database: 'test', entities: [User], synchronize: true, }), }), UsersModule, ], controllers: [AppController], providers: [AppService], }) export class AppModule { }
UsersModule
import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { UsersController } from './users.controller'; import { UsersService } from './users.service'; import { User } from './entities/user.entity'; import { UserRepository } from './users.repository'; @Module({ imports: [TypeOrmModule.forFeature([User])], controllers: [UsersController], providers: [UserRepository, UsersService], }) export class UsersModule { }
UsersService
import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { User } from './entities/user.entity'; import { CreateUserParams } from './utils/types/types'; @Injectable() export class UsersService { constructor(@InjectRepository(User) private userRepository: Repository<User>) { } async findOne(username: string): Promise<User | undefined> { return this.userRepository.findOne({ where: { username } }); } async findAll(): Promise<User[]> { return this.userRepository.find(); } createUser(userDetails: CreateUserParams) { const newUser = this.userRepository.create({ ...userDetails, createdAt: new Date() }); return this.userRepository.save(newUser); } }
内容的提问来源于stack exchange,提问作者ChristmasFighters
相关产品推荐
相关产品推荐

