设置Docker和Vite

mtb9vblg  于 2023-05-06  发布在  Docker
关注(0)|答案(1)|浏览(197)

我最近将一个小的exercice项目从CRA迁移到Vite,它使用Docker,但是由于Vite是作为Dev依赖安装的,我正在运行npm run install --omit=dev,所以它可以解释为什么我会得到这个错误:

> #12 0.726 > npm run build --prefix client
> #12 0.726
> #12 1.193
> #12 1.193 > project@2.0.0 build
> #12 1.193 > vite build --emptyOutDir
> #12 1.193
> #12 1.199 sh: vite: not found

那么,有没有一种方法可以只安装Vite并忽略生产中不需要的所有其余依赖项,或者我必须使用默认的npm run install正常安装所有包?Docker文件如下:

FROM node:lts-alpine

WORKDIR /app

COPY package*.json ./

COPY client/package*.json client/
RUN npm run install-client --omit=dev

COPY server/package*.json server/
RUN npm run install-server --omit=dev

COPY client/ client/
RUN  npm run build

COPY server/ server/

USER node

CMD [ "npm", "start", "--prefix", "server" ]

EXPOSE 8000
jk9hmnmh

jk9hmnmh1#

您可以尝试安装viteglobally并使用multi-stage构建来从node_modules文件夹中排除vite依赖项:

FROM node:lts-alpine as builder

WORKDIR /app
# vite will be installed in npm global directory
RUN npm install -g vite
COPY package*.json ./
COPY client/package*.json client/
RUN npm run install-client --omit=dev
COPY server/package*.json server/
RUN npm run install-server --omit=dev
COPY client/ client/
RUN  npm run build
COPY server/ server/

FROM node:lts-alpine
# copy built files from builder image to new clean node image without vite
COPY --from=builder /app /app
WORKDIR /app

USER node

CMD [ "npm", "start", "--prefix", "server" ]

EXPOSE 8000

相关问题