1👍
The reason why your docker build is failing with this message
sh: 1: vite: not found
is because you are using NODE_ENV=production
which causes npm install
to skip any devDependencies
, but in your package.json
vite
is listed as a devDependency
, so the npm run build
will fail.
One approach would be to split the Dockerfile into two stages:
- First stage where you execute
npm run build
- Second stage where you copy the build artifacts from stage 1 and run the application
This would look something like this:
FROM node:21-alpine AS build-stage
WORKDIR /app
COPY package*.json .
RUN npm ci
COPY . .
RUN npm run build
RUN npm prune --production
FROM node:21-alpine
WORKDIR /app
COPY --from=build-stage /app/build build/
COPY --from=build-stage /app/node_modules node_modules/
COPY package.json .
EXPOSE 3000
ENV NODE_ENV=production
CMD [ "node", "build" ]
Source:stackexchange.com