【问题标题】:Dependencies (RxJS) not being installed from package.json in Dockerfile?没有从 Dockerfile 中的 package.json 安装依赖项(RxJS)?
【发布时间】:2020-12-27 05:47:19
【问题描述】:

我正在尝试将我的应用容器化以将其部署到 GCP。在设置任何触发器之前,我一直在进行一些本地测试,出于某种原因,尽管在 package.json 中列出了 rxjs(可能还有其他),但并未安装。

在我的 Dockerfile 中,它在构建步骤中出错,因为它找不到 rxjs 模块(RUN ls node_modules 表明即使在安装后它也不存在)。我已经明确尝试添加RUN npm install rxjs,但即使这样也不起作用。当我这样做时,我得到了一堆关于无法找到由 npm 暂存的模块的其他错误。这既发生在我的本地机器上,也发生在我尝试构建/部署的 Cloud Run 实例上。

在任何人提到它之前,我已经尝试删除我的 package-lock.json 并进行全新安装 - 这具有完全相同的结果。当我实际上直接从命令行构建它时,一切正常。这似乎更像是 Docker 容器的问题。

Dockerfile

FROM node:12.18-alpine AS dev

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install --only=dev

COPY . .

RUN npm run build:prod

FROM node:12.18-alpine AS prod

ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install --only=prod

COPY . .

COPY --from=dev /usr/src/app/dist ./dist

CMD ["npm", "run", "start"]

package.json

{
  "name": "ui",
  "version": "1.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "build:prod": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "dependencies": {
    "@angular/animations": "~9.1.7",
    "@angular/cdk": "^9.2.4",
    "@angular/common": "~9.1.7",
    "@angular/compiler": "~9.1.7",
    "@angular/core": "~9.1.7",
    "@angular/forms": "~9.1.7",
    "@angular/http": "^7.2.16",
    "@angular/material": "^9.2.4",
    "@angular/platform-browser": "~9.1.7",
    "@angular/platform-browser-dynamic": "~9.1.7",
    "@angular/router": "~9.1.7",
    "@ngstack/code-editor": "^1.0.0",
    "angular-markdown-editor": "^2.0.2",
    "hammerjs": "^2.0.8",
    "ngx-markdown": "^10.1.1",
    "rxjs": "~6.5.4",
    "tslib": "^1.10.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.901.7",
    "@angular/cli": "~9.1.6",
    "@angular/compiler-cli": "~9.1.7",
    "@types/hammerjs": "^2.0.36",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "codelyzer": "^5.1.2",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.0",
    "karma-jasmine": "~3.0.1",
    "karma-jasmine-html-reporter": "^1.4.2",
    "protractor": "~5.4.3",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~3.8.3"
  }
}

来自docker build --rm .的输出

$ docker build --rm .
Sending build context to Docker daemon  868.4kB
Step 1/15 : FROM node:12.18-alpine AS dev
 ---> 18f4bc975732
Step 2/15 : WORKDIR /usr/src/app
 ---> Using cache
 ---> be5e328970e5
Step 3/15 : COPY package*.json ./
 ---> Using cache
 ---> 634457d1a251
Step 4/15 : RUN npm install --only=dev
 ---> Using cache
 ---> 4e8cd1472043
Step 5/15 : COPY . .
 ---> Using cache
 ---> 423a944a84c2
Step 6/15 : RUN npm run build:prod
 ---> Running in b68a5b25ff84

> ui@1.0.0 build:prod /usr/src/app
> ng build --prod

Unknown error: Error: Cannot find module 'rxjs'
Require stack:
- /usr/src/app/node_modules/inquirer/lib/ui/prompt.js
- /usr/src/app/node_modules/inquirer/lib/inquirer.js
- /usr/src/app/node_modules/@angular/cli/models/analytics.js
- /usr/src/app/node_modules/@angular/cli/models/command-runner.js
- /usr/src/app/node_modules/@angular/cli/lib/cli/index.js
- /usr/src/app/node_modules/@angular/cli/lib/init.js
- /usr/src/app/node_modules/@angular/cli/bin/ng
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! ui@1.0.0 build:prod: `ng build --prod`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the ui@1.0.0 build:prod script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-09-08T19_47_14_999Z-debug.log
The command '/bin/sh -c npm run build:prod' returned a non-zero code: 1

【问题讨论】:

    标签: node.js angular docker npm


    【解决方案1】:

    第 4/15 步:运行 npm install --only=dev ---> 使用缓存

    只要您在构建过程中遇到一些问题,我建议您在不使用缓存的情况下进行构建。只需添加 --no-cache 以确保确实调用了 npm install

    docker build --no-cache -t mybuild:v1 .  
    

    【讨论】:

    • 这对我造成了同样的错误。我应该将此添加到问题中(现已更新),但即使我尝试远程测试构建,GCP 也会在完全相同的点抛出相同的错误。
    • 好吧。另一个想法。当您使用标志--only=dev 运行 npm install 时,您将安装 devDependencies 下的所有包。这意味着您拥有的 dependencies rxjs 不会被安装,因此你无法成功调用npm run build:prod。在将 dependencies 添加到您的 dockerized 应用程序后,您将能够进行构建。我看到两种可能性:1. 尝试在 RUN npm install --only=prod 之后移动 RUN npm run build:prod 或 2. 使用不带标志的 npm install,因此所有依赖项都将得到满足
    • 这完全有道理。我选择了第二种解决方案,它对我有用。尽管我在流程的部署步骤中遇到了另一个错误,但我最初的问题得到了回答。谢谢!
    猜你喜欢
    • 2016-08-02
    • 2022-12-24
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多