错误:需要以独立模式安装Sharp才能优化图像

问题描述 投票:0回答:1

Dockerfile:

# Build Image - Stage 1
FROM node:20-alpine AS base

# Stage 2 - Install Dependencies
# Install dependencies only when needed
FROM base AS deps

# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat vips-dev python3 make g++

WORKDIR /app

# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .npmrc ./
RUN \
    if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
    elif [ -f package-lock.json ]; then npm ci; \
    elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
    else echo "Lockfile not found." && exit 1; \
    fi

# Stage 3 - Build Application
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# If using npm comment out above and use below instead
RUN npm run build:projectx-ui

# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

USER nextjs

COPY --from=builder --chown=nextjs:nodejs /app/dist/apps/projectx-ui/public ./apps/projectx-ui/public

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/dist/apps/projectx-ui/.next/standalone/ ./
COPY --from=builder --chown=nextjs:nodejs /app/dist/apps/projectx-ui/.next/static ./dist/apps/projectx-ui/.next/static

# Expose port 4000 for the application
EXPOSE 4000

# Add the port as 
ENV PORT 4000

# Start the application
# CMD ["npm", "start", "--", "--port", "4000"]
CMD ["node", "apps/projectx-ui/server.js"]

我正在使用 NX monorepo 工具。 Dockerfile 已添加到根目录。我已经安装了锐利。我遇到了几个 GitHub 问题,但没有一个解决方案有效。我还尝试设置 Nextjs 文档中提到的环境变量。但是,没有成功。

每当我从 docker 映像启动服务器并在本地计算机上打开 URL 时,我都会看到错误“错误:需要以独立模式安装 Sharp 以进行图像优化”

我可以看到该模块可用为

sharp-linuxmusl-arm64v8.node

我在部署应用程序时看到同样的错误。任何帮助将不胜感激。

docker npm image-processing next.js nrwl-nx
1个回答
0
投票

添加

ENV NEXT_SHARP_PATH=/app/node_modules/sharp
在您的 Dockerfile 中
ENV NODE_ENV production
之后。

文档说要添加

ENV NEXT_SHARP_PATH=/tmp/node_modules/sharp
但如果您检查模块在容器中的位置,您会看到它们位于
/app
中。

© www.soinside.com 2019 - 2024. All rights reserved.