常见错误
在学习和使用 NestJS 的过程中,你可能会遇到本页描述的错误。
"Cannot resolve dependency" 错误
NestJS 开发工具 可以帮助你解决 "Cannot resolve dependency" 错误。
最常见的错误信息是 Nest 无法解析某个提供者的依赖。它通常长这样:
Nest can't resolve dependencies of the <provider> (?). Please make sure that the argument <unknown_token> at index [<index>] is available in the <module> module.
Potential solutions:
- Is <module> a valid NestJS module?
- 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> */ ]
})
For more common dependency resolution issues, see: https://docs.nestjs.com/faq/common-errors最常见的原因是 <unknown_token> 在 <module> 中不可用:它既没有列在模块的 providers 数组中,也没有被其 imports 数组中的某个模块导出。请确认该提供者已经注册,并遵循 NestJS 提供者的标准做法。
另一个常见错误是把提供者放进模块的 imports 数组。如果该类用 @Injectable() 装饰过,Nest 会在启动时检测到这一点,并抛出错误指出该类不能出现在 "imports" 数组中;请把它移到 providers 数组。
遇到这个错误时,打开错误信息中提到的模块,检查它的 providers。对 providers 数组中的每个提供者,都要确认该模块能访问它的全部依赖。提供者常常在功能模块和根模块中重复出现,这会让 Nest 尝试实例化该提供者两次。多数情况下,你应当删除重复的那份,改为把包含 <provider> 的模块添加到根模块的 imports 数组中。
另外还要确认你没有把提供者注入到它自身:NestJS 不允许自注入。发生这种情况时,<unknown_token> 通常就等于 <provider>。
如果报错索引位置上的依赖在运行时解析为 undefined 或 Object,错误信息会省略标记名,改为指向当前模块,并列出一组不同的可能原因:
Nest can't resolve dependencies of the <provider> (?). Please make sure that the argument at index [<index>] is available in the current module.
Potential solutions:
- The dependency at index [<index>] appears to be undefined at runtime
- This commonly occurs when using 'import type' instead of 'import' for injectable classes
...这通常由以下原因之一导致:
- 循环文件导入。 与下文所述的循环依赖不同,这里的提供者在构造函数中并不依赖彼此,而是两个文件互相导入。例如,某个模块文件声明了一个标记并导入了一个提供者,而该提供者又从该模块文件导入这个标记常量。如果使用汇总文件(barrel files),请确保这些汇总导入不会造成此类循环导入。
- 按类型或接口注入。 类型和接口在运行时并不存在,因此 Nest 无法把它们用作注入标记。请改为注入类引用,或使用带
@Inject()装饰器的自定义标记(参见自定义提供者)。 - 仅类型导入。 对于基于类的提供者,请导入具体的类,而不是只用
import type ...语法导入其类型。
在 monorepo 配置 中,你可能会看到同样的错误,其中 <unknown_token> 是核心的 ModuleRef 提供者:
Nest can't resolve dependencies of the <provider> (?).
Please make sure that the argument ModuleRef at index [<index>] is available in the <module> module.
...这通常发生在项目加载了两份 @nestjs/core 包时,如下所示:
.
├── package.json
├── apps
│ └── api
│ └── node_modules
│ └── @nestjs/bull
│ └── node_modules
│ └── @nestjs/core
└── node_modules
├── (other packages)
└── @nestjs/core解决办法:
- 对于 Yarn 工作区,请使用 nohoist 特性 来阻止
@nestjs/core包被提升(hoist)。 - 对于 pnpm 工作区,请在另一个模块中把
@nestjs/core声明为对等依赖,并在导入该模块的应用的 package.json 里设置"dependenciesMeta": {{ '{' }}"other-module-name": {{ '{' }}"injected": true }}。参见 pnpm 文档中的 dependenciesMeta.injected。
"Circular dependency" 错误
有时你会发现应用中很难避免循环依赖,需要帮 Nest 解决它们。由循环依赖引起的错误如下:
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>]
# 示例链路 AppModule -> FooModule循环依赖可能源于彼此依赖的提供者,也可能源于为常量而彼此依赖的 TypeScript 文件(例如模块文件导出了一个常量,服务文件导入了它)。对后一种情况,请把常量移到单独的文件中。对前一种情况,请遵循循环依赖指南,并确保模块和提供者都用 forwardRef() 包裹。
调试依赖错误
除了手动核对依赖之外,你还可以把 NEST_DEBUG 环境变量设为任意非空字符串,以便在 Nest 解析应用依赖时获得额外的日志输出。

在上图中,黄色的字符串是正在被注入的依赖所在的宿主类,蓝色的字符串是被注入依赖的名称(或注入标记),紫色的字符串是 Nest 搜索该依赖时所在的模块。借助这些输出,你通常可以追踪依赖解析的过程,找出注入失败的原因。
"File change detected" 无限循环
使用 TypeScript 4.9 或更高版本的 Windows 用户可能会遇到这个问题。以监听模式运行应用时(例如 npm run start:dev),你会看到下面这条日志无限循环地出现:
XX:XX:XX AM - File change detected. Starting incremental compilation...
XX:XX:XX AM - Found 0 errors. Watching for file changes.用 Nest CLI 以监听模式启动应用时,它会以监听模式运行 TypeScript 编译器(相当于 tsc --watch)。TypeScript 4.9 引入了一种新策略来检测文件变化,这很可能就是该问题的原因。
要解决它,请在 tsconfig.json 文件的 "compilerOptions" 选项之后加入下面的设置:
"watchOptions": {
"watchFile": "fixedPollingInterval"
}这会告诉 TypeScript 改为轮询文件变化,而不是使用文件系统事件(自 4.9 起的默认方式),后者在某些机器上会引发问题。
要了解更多,请参见 TypeScript 文档中的 watchFile 选项。