🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 常见错误 在使用Nestjs开发时,可能遇到不同错误。 ### `Cannot resolve dependency(无法处理依赖)`错误 最常见的错误信息可能是Nest无法处理提供者依赖,这个错误看上去往往像这样: ```bash Nest can't resolve dependencies of the <provider> (?). Please make sure that the argument <unknown_token> at index [<index>] is available in the <module> context. Potential solutions: - If <unknown_token> is a provider, is it part of the current <module>? - If <unknown_token> is exported from a separate @Module, is that module imported within <module>? @Module({ imports: [ /* the Module containing <unknown_token> */ ] }) ``` 这个错误最常用的原因,是提供者没有列在模块的`provider`数组中。确保提供者在提供者数组中并且遵循以下NestJs提供者实践。 有一些常规的错误。其中一个是把提供者放到了`import`数组里。在这种情况下,错误会是提供者的名字在`<module>`应该在的地方。 如果你在开发过程中遇到这些错误,看看提到的出错信息以及其提供者。确保提供者在提供者数组中,模块可以访问所有的依赖。有时提供者在"Feature Module"和"Root Module"中重复,这意味着Nest将尝试实例化提供者两次。更可能的是,从模块复制的提供者应该替换为在"Root Module"的`imports`数组中引入。 ### `Circular dependency(循环依赖)`错误 有时你会发现在应用中无法避免循环依赖。你可能需要做一些工作来帮助Nest解决它,循环依赖错误看上去可能像这样: ```bash Nest cannot create the <module> instance. The module at index [<index>] of the <module> "imports" array is undefined. Potential causes: - A circular dependency between modules. Use forwardRef() to avoid it. Read more: https://docs.nestjs.com/fundamentals/circular-dependency - The module at index [<index>] is of type "undefined". Check your import statements and the type of the module. Scope [<module_import_chain>] # example chain AppModule -> FooModule ``` ### 调试依赖错误 除了手动验证您的依赖项是否正确之外,从 Nest 8.1.0 开始,您可以将“NEST\_DEBUG”环境变量设置为解析为真值的字符串,并在 Nest 解析应用程序的所有依赖项时获取额外的日志信息。 ![](https://docs.nestjs.com/assets/injector_logs.png) 在上图中,黄色字符串是被注入依赖的宿主类,蓝色字符串是注入依赖的名称,或者它的注入令牌,紫色字符串是依赖所在的模块搜索。使用它,您通常可以追溯发生了什么以及为什么会出现依赖注入问题的依赖解析。 循环依赖错误可能来自提供者间的相互依赖,或者TypeScript文件常量相互依赖,例如从一个模型文件中导出常量而在一个服务中引入他们。在后一种情况下,建议为常量创建一个独立的文件。在前一种情况下,查看循环依赖指导并保证两个模块和提供者都标记有`forwarRef`。