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

使用pnpm运行Turborepo Docker镜像时触发MODULE_NOT_FOUND错误

问题:Docker运行Turborepo+pnpm项目时出现MODULE_NOT_FOUND错误

运行Docker镜像时触发以下错误:

node:internal/modules/cjs/loader:1080
Error: Cannot find module '/app/apps/backend/pnpm'
code: 'MODULE_NOT_FOUND', exited with code 1

原本使用yarn管理依赖,现切换为pnpm运行Turborepo项目,当前使用的Dockerfile和docker-compose.yml配置如下:

Dockerfile

FROM node:18 AS base

FROM base AS builder

RUN apk add --no-cache libc6-compat
RUN apk update
# Set working directory
WORKDIR /app
RUN npm install  -g turbo
COPY . .
RUN turbo prune --scope=api --docker  
# here --scope=api is the name of the workspace. that is api is the name of the workspace in the package.json

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk add --no-cache libc6-compat
RUN apk update
WORKDIR /app

# First install dependencies (as they change less often)
COPY .gitignore .gitignore
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/pnpm.lock.yml ./pnpm-lock.yml
RUN npm install -g pnpm

RUN pnpm install --from-lock-file --frozen-lockfile

# Build the project and its dependencies
COPY --from=builder /app/out/full/ .
RUN pnpm dlx prisma generate
COPY turbo.json turbo.json
RUN pnpm dlx turbo run build --filter=api...


FROM base AS runner
WORKDIR /app

# Don't run production as root
RUN addgroup --system --gid 1001 expressjs
RUN adduser --system --uid 1001 expressjs
USER expressjs
COPY --from=installer /app .

WORKDIR /app/apps/api

CMD ["pnpm", "start"]

docker-compose.yml

services:
  postgres:
    image: postgres:15-alpine
    ports:
      - 5432-5432
    environment:
      POSTGRES_DB: backend
      POSTGRES_HOST_AUTH_METHOD: trust
  redis:
    image : redis:6.2.6-alpine
    
  backend:
    build:
      context: ./apps/api/Dockerfile
      dockerfile: .
    ports:
      - 5000:5000
      - 9229:9229 # for debugging
    volumes:
      - ./apps/api:/usr/src/app # mount the current directory to the container ( which is a working directory mentioned in the Dockerfile)
      - /usr/src/app/node_modules
      - /usr/src/app/.pnpm-store
    command: pnpm start:docker
    depends_on:
      - postgres # before starting the backend, start the postgres service
    environment:
      DATABASE_URL: ***
      PORT: 5000
      REDIS_HOST: ****

解决方案

1. 修正docker-compose的构建上下文与路径映射

  • 修复构建上下文:当前context设为./apps/api/Dockerfile错误,Turborepo需要读取根目录的turbo.json和workspace配置,应修改为:
    backend:
      build:
        context: .
        dockerfile: ./apps/api/Dockerfile
    
  • 对齐路径挂载:Dockerfile中WORKDIR为/app,但挂载目标是/usr/src/app,导致目录结构混乱,调整卷挂载:
    volumes:
      - ./apps/api:/app/apps/api
      - /app/node_modules
      - /app/.pnpm-store
    

2. 确保runner阶段能访问pnpm

切换非root用户后,全局pnpm可能不在PATH中,可通过以下方式解决:

  • 使用npx调用pnpm:
    CMD ["npx", "pnpm", "start"]
    
  • 或者在installer阶段执行RUN which pnpm确认全局路径,然后在CMD中使用绝对路径(例如/usr/local/bin/pnpm):
    CMD ["/usr/local/bin/pnpm", "start"]
    
  • 也可以将pnpm安装到项目本地,在installer阶段添加:
    RUN pnpm add -D pnpm
    
    然后runner阶段调用本地pnpm:
    CMD ["./node_modules/.bin/pnpm", "start"]
    

3. 修正路径与workspace匹配

错误信息中出现/app/apps/backend/pnpm,但配置中workspace是api,需确认:

  • 项目根目录package.json的workspaces是否正确包含apps/api
  • Dockerfile中的WORKDIR和CMD路径是否与实际项目结构一致,避免出现backend这类错误路径

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 04:02:11