You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Yarn 3搭建NestJs项目时出现模块及类型声明找不到的问题

解决NestJS项目中Cannot find module '@nestjs/common'类报错

以下是针对Yarn 3环境下该问题的几种解决方法,按优先级尝试:

方法一:清除缓存并重新安装依赖

Yarn缓存异常可能导致依赖未正确落地,执行以下命令:

yarn cache clean
yarn install --check-files

--check-files参数会强制校验本地文件与依赖清单的一致性,确保所有依赖正确安装。

方法二:手动补全核心依赖

如果package.json中缺失NestJS核心依赖,手动安装:

# 安装生产依赖
yarn add @nestjs/common @nestjs/core @nestjs/platform-express reflect-metadata rxjs
# 安装开发依赖
yarn add -D @nestjs/cli @nestjs/schematics @types/node typescript ts-loader ts-node tsconfig-paths

方法三:适配Yarn 3的PnP模式

Yarn 3默认启用PnP(Plug'n'Play)替代传统node_modules,TypeScript原生解析无法识别PnP模块路径,需配置插件支持:

  1. 安装PnP TypeScript插件:
yarn add -D @yarnpkg/pnpify
  1. 修改tsconfig.json的compilerOptions,添加插件配置:
{
  "compilerOptions": {
    // 保留原有配置,新增以下内容
    "plugins": [{"name": "@yarnpkg/pnpify/typescript-plugin"}]
  }
}
  1. 使用pnpify包裹启动命令:
yarn pnpify yarn start:dev

方法四:切换回传统node_modules模式(可选)

如果不想使用PnP,可禁用该模式:

yarn config set nodeLinker node-modules
yarn install

内容的提问来源于stack exchange,提问作者jesusrlm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 21:15:37