【问题标题】:Docker Container NPM Dependencies Not Found未找到 Docker 容器 NPM 依赖项
【发布时间】:2019-11-16 12:49:15
【问题描述】:

我为我的 docker 容器设置了一个两阶段的构建过程。第一阶段安装 git 并运行 npm install。第二阶段从第一阶段复制 node_modules,然后从我的主机复制源代码。然后它运行开发服务器。一切似乎都正常工作,只是当开发服务器尝试启动时会抛出此错误:

"frontend_1 | 未找到此依赖项:
frontend_1 |
frontend_1 | * vue-free-transform in ./node_modules/cache-loader/dist/cjs.js??ref--12-0 !./node_modules/babel-loader/lib!./node_modules/vuetify-loader/lib/loader.js!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules /vue-loader/lib??vue-loader-options!./src/components/editor/Line.vue?vue&type=script&lang=js&"

我执行到容器中并检查了 node_modules 文件夹,确定它位于正确的位置,并且 vue-free-transform 在那里并且看起来完好无损。该项目是一个 vue cli 3 项目,所有 webpack/bower 设置都保留在 vue cli 默认值。

我已经尝试删除所有使用的卷,清除 npm 缓存,手动执行到容器中并运行 npm install。

Dockerfile:

FROM node:alpine as builder

WORKDIR /usr/src/app
COPY package.json .
RUN apk add --no-cache git
RUN npm install

FROM node:alpine

WORKDIR /usr/src/app
COPY --from=builder /usr/src/app/node_modules ./node_modules
COPY . .

CMD ["npm", "run", "serve"]

码头工人撰写:

version: '3'

services:
  frontend:
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - "8080:8080"
#    volumes:
#      - /usr/src/app/node_modules
#      - .:/usr/src/app
    command: ["npm", "run", "serve"]

我希望开发服务器能够毫无问题地启动,但我却收到了上面的错误消息。如果我缺少任何重要的细节,请告诉我!

【问题讨论】:

  • 在你的 Dockerfile 中,从构建器复制过来之后,你是否应该不再运行 npm install
  • 我不这么认为。构建器步骤的目的是运行 npm install,然后将完整的 node_modules 文件夹复制到最终容器中。我这样做的原因是为了避免必须在最终映像中安装 git(我的一些 npm 包是 repos)。

标签: docker vue.js npm docker-compose


【解决方案1】:

嗯,我想通了……像往常一样,这只是一个愚蠢的错误。 repo 被拉得很好,但该模块从未构建过,因此其中没有 dist 文件夹。

必须确保将 dist 文件夹与模块一起提交,或者在使用 npm 安装后构建它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-29
    • 2019-10-28
    • 2017-01-27
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    相关资源
    最近更新 更多