Turborepo+PNPM+Docker项目依赖版本不匹配问题求助
问题:Turborepo + PNPM Docker构建时无法找到workspace依赖
eslint-config-custom@* 错误日志
#0 0.732 @types/node is linked to /app/node_modules from /node_modules/.pnpm/@types+node@17.0.45/node_modules/@types/node #0 0.733 @types/react is linked to /app/node_modules from /node_modules/.pnpm/@types+react@18.0.26/node_modules/@types/react #0 0.733 @types/react-dom is linked to /app/node_modules from /node_modules/.pnpm/@types+react-dom@18.0.10/node_modules/@types/react-dom #0 0.733 eslint is linked to /app/node_modules from /node_modules/.pnpm/eslint@7.32.0/node_modules/eslint #0 0.733 typescript is linked to /app/node_modules from /node_modules/.pnpm/typescript@4.9.4/node_modules/typescript #0 0.734 next is linked to /app/node_modules from /node_modules/.pnpm/next@13.0.0_pjwopsidmaokadturxaafygjp4/node_modules/next #0 0.734 react is linked to /app/node_modules from /node_modules/.pnpm/react@18.2.0/node_modules/react #0 0.734 react-dom is linked to /app/node_modules from /node_modules/.pnpm/react-dom@18.2.0_react@18.2.0/node_modules/react-dom #0 0.734 react-query is linked to /app/node_modules from /node_modules/.pnpm/react-query@3.39.2_biqbaboplfbrettd7655fr4n2y/node_modules/react-query #0 0.741 ERR_PNPM_NO_MATCHING_VERSION_INSIDE_WORKSPACE In : No matching version found for eslint-config-custom@* inside the workspace #0 0.741 #0 0.741 This error happened while installing a direct dependency of /app ------ failed to solve: executor failed running [/bin/sh -c pnpm install]: exit code: 1 make: *** [local] Error 17
相关配置文件
apps/web/package.json
{ "name": "web", "version": "0.0.0", "private": true, "scripts": { "dev": "next dev --port 3002", "build": "next build", "start": "next start", "lint": "next lint" }, "dependencies": { "next": "13.0.0", "react": "18.2.0", "react-dom": "18.2.0", "react-query": "^3.39.2", "ui": "workspace:*" }, "devDependencies": { "@babel/core": "^7.0.0", "@types/node": "^17.0.12", "@types/react": "^18.0.22", "@types/react-dom": "^18.0.7", "eslint": "7.32.0", "eslint-config-custom": "workspace:*", "tsconfig": "workspace:*", "typescript": "^4.5.3" } }
Makefile
local: @docker-compose stop && docker-compose up --build --remove-orphans;
docker-compose.yaml
version: "3.9" services: frontend: container_name: frontend build: context: ./apps/web dockerfile: Dockerfile.local restart: always env_file: .env ports: - "${FRONTEND_PORT}:${FRONTEND_PORT}" networks: - gh-network command: "npm start" backend: container_name: backend build: context: ./apps/server dockerfile: Dockerfile.local restart: always env_file: .env volumes: - ./apps/server:/svr/app - "./scripts/wait.sh:/wait.sh" - /svr/app/node_modules networks: - gh-network ports: - "${BACKEND_PORT}:${BACKEND_PORT}" depends_on: - gh-pg-db links: - gh-pg-db gh-pg-db: image: postgres:12-alpine restart: always container_name: gh-pg-db env_file: - .env environment: POSTGRES_PASSWORD: ${DB_PASSWORD} PGDATA: /var/lib/postgresql/data POSTGRES_USER: ${DB_USER} POSTGRES_DB: ${DB_NAME} ports: - "${DB_PORT}:${DB_PORT}" volumes: - pgdata:/var/lib/postgresql/data networks: - gh-network pgadmin-portal: image: dpage/pgadmin4 restart: always container_name: pgadmin-portal env_file: - .env environment: PGADMIN_DEFAULT_PASSWORD: "${PGADMIN_DEFAULT_PASSWORD}" PGADMIN_DEFAULT_EMAIL: "${PGADMIN_DEFAULT_EMAIL}" volumes: - pgadmin:/root/.pgadmin ports: - "${PGADMIN_PORT}:80" depends_on: - gh-pg-db networks: - gh-network volumes: pgdata: pgadmin: networks: gh-network: driver: bridge
apps/web/Dockerfile.local
FROM node:14-alpine RUN npm i -g pnpm RUN pnpm --version WORKDIR /app COPY . . RUN pnpm install
已尝试的操作
- 将web包中eslint版本(7.32.0)与共享包的eslint版本(7.23.0)修改为一致,问题未解决。
解决方案
核心问题原因
Docker构建前端时,context设置为./apps/web,导致仅复制了web应用的文件,而Turborepo的workspace依赖(eslint-config-custom、tsconfig、ui)位于项目根目录的packages文件夹中,同时缺少根目录的pnpm-workspace.yaml文件,pnpm无法识别整个工作空间结构,因此找不到指定的workspace依赖。
步骤1:修改docker-compose.yaml中的前端构建配置
将frontend服务的build context改为项目根目录,同时指定dockerfile的完整路径:
services: frontend: container_name: frontend build: context: . # 改为项目根目录 dockerfile: ./apps/web/Dockerfile.local # 指定web的Dockerfile路径 # 其余配置保持不变
步骤2:修改apps/web/Dockerfile.local,仅复制必要文件
避免复制整个项目,只复制web应用、共享包和workspace配置文件:
FROM node:14-alpine RUN npm i -g pnpm RUN pnpm --version WORKDIR /app # 先复制workspace配置文件,确保pnpm能识别工作空间 COPY pnpm-workspace.yaml package.json ./ # 复制共享包目录 COPY packages ./packages # 复制web应用目录 COPY apps/web ./apps/web # 安装依赖并构建web应用 RUN pnpm install # 可选:提前构建Next应用,避免启动时再构建 RUN pnpm run --filter web build # 启动应用 CMD ["pnpm", "run", "--filter", "web", "start"]
步骤3:优化Docker构建(可选)
使用多阶段构建减少镜像体积:
# 构建阶段 FROM node:14-alpine AS builder RUN npm i -g pnpm WORKDIR /app COPY pnpm-workspace.yaml package.json ./ COPY packages ./packages COPY apps/web ./apps/web RUN pnpm install RUN pnpm run --filter web build # 运行阶段 FROM node:14-alpine WORKDIR /app COPY --from=builder /app/apps/web/.next ./apps/web/.next COPY --from=builder /app/apps/web/public ./apps/web/public COPY --from=builder /app/node_modules ./node_modules COPY --from=builder /app/package.json ./package.json CMD ["pnpm", "run", "--filter", "web", "start"]
验证修改
执行make local命令,Docker将正确识别Turborepo工作空间结构,安装所有workspace依赖,成功构建并启动web应用,访问http://localhost:3002即可看到初始化的Next/React页面。
内容的提问来源于stack exchange,提问作者daniel blythe
相关产品推荐
相关产品推荐

