Next.js Discord

Discord Forum

Sharp issues in production environment

Answered
Bushtit posted this in #help-forum
Open in Discord
BushtitOP
Whatever I do, I can not get image optimization to work in my application. It always complains that sharp is missing.

This is my package.json:
{
  "name": "application",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "debug": "env NODE_OPTIONS='--inspect' next",
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "bcrypt": "^5.1.0",
    "cron": "^2.3.0",
    "dayjs": "^1.11.7",
    "eslint": "8.28.0",
    "eslint-config-next": "13.0.5",
    "mysql2": "3.6.5",
    "next": "14.0.4",
    "next-auth": "^4.24.5",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "sharp": "^0.33.1",
    "swr": "^2.2.4"
  },
  "devDependencies": {
    "@types/bcrypt": "^5.0.0",
    "@types/cron": "^2.0.1",
    "@types/node": "^20.3.3",
    "@types/react": "^18.2.0",
    "@types/smtp-server": "^3.5.7",
    "@typescript-eslint/eslint-plugin": "^6.16.0",
    "@typescript-eslint/parser": "^6.0.0",
    "eslint-config-prettier": "^8.5.0",
    "typescript": "^5.3.3"
  }
}


(See docker file in post below)

This is the error message that I get when executing running the container:
⨯ Error: 'sharp' is required to be installed in standalone mode for the image optimization to function correctly. Read more at: https://nextjs.org/docs/messages/sharp-missing-in-production
Answered by Ray
hi can you try install sharp@0.32.6
View full answer

5 Replies

BushtitOP
And here is my docker file:
# Install dependencies only when needed
FROM node:20-alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile

# Rebuild the source code only when needed
FROM node:20-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN yarn build

# Production image, copy all the files and run next
FROM node:20-alpine AS runner
WORKDIR /app

ENV NODE_ENV production

RUN addgroup --system --gid 1001 somegroup
RUN adduser --system --uid 1001 someuser

COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/next.config.js ./

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

RUN mkdir -p /app/.next/cache/images

RUN chown -R 1001:1001 .next

USER someuser

EXPOSE 3000

ENV PORT 3000

CMD ["node", "server.js"]
Answer
@Ray hi can you try install ` sharp@0.32.6 `
BushtitOP
Golly jee that worked!
Thanks a bunch matey 🙂