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

Turborepo Monorepo中引入@repo/example模块报错求助

问题描述

在Turborepo monorepo项目里搭了个@repo/example包,写import {} from '@repo/example';时VS Code报错:

Cannot find module '@repo/example' or its corresponding type declarations.

已经确认node_modules里有@repo/example文件夹和所有文件,完全跟着教程操作还是出问题。相关配置如下:

@repo/example 的 package.json

{
  "name": "@repo/example",
  "exports": {
    ".": "./src/index.ts"
  },
  "dependencies": {
    "@repo/example": "file:"
  },
  "devDependencies": {
    "@repo/typescript-config": "workspace:*"
  }
}

@repo/example 的 tsconfig.json

{
  "extends": "@repo/typescript-config/base.json",
  "includes": ["src"],
  "exclude": ["node_modules"]
}

@repo/example 的 src/index.ts

export const turborepo = 'Turborepo!';
解决办法

1. 删除无效的循环依赖

@repo/example的package.json里,dependencies里写了自己依赖自己@repo/example: "file:",这完全没必要,直接删掉这一行:

// 修改后示例
{
  "name": "@repo/example",
  "exports": {
    ".": "./src/index.ts"
  },
  "devDependencies": {
    "@repo/typescript-config": "workspace:*"
  }
}

2. 给exports补上类型入口

TypeScript需要明确的类型声明路径,在exports里加个types字段:

{
  "name": "@repo/example",
  "exports": {
    ".": {
      "types": "./src/index.ts",
      "default": "./src/index.ts"
    }
  },
  "devDependencies": {
    "@repo/typescript-config": "workspace:*"
  }
}

3. 配置消费项目的tsconfig

在要导入@repo/example的项目(比如apps下的项目)的tsconfig.json里,加上paths配置让TS能找到这个包:

{
  "extends": "@repo/typescript-config/base.json",
  "compilerOptions": {
    "paths": {
      "@repo/example": ["../packages/example/src/index.ts"]
    }
  },
  "includes": ["src"]
}

4. 重新装依赖+触发构建

执行命令更新依赖,要是包有build脚本就先跑一遍:

pnpm install # 换成你用的包管理工具:npm install/yarn install
turbo run build

5. 重启VS Code的TS服务

按Ctrl+Shift+P(Windows/Linux)或者Cmd+Shift+P(Mac),搜TypeScript: Restart TS Server重启服务,让VS Code重新识别模块。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 13:44:59