[英]Checking if package.json dependencies match the installed dependencies
[英]Dependencies (RxJS) not being installed from package.json in Dockerfile?
我正在嘗試將我的應用程序容器化以將其部署到 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 .
$ 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
步驟 4/15:運行 npm install --only=dev ---> 使用緩存
只要您在進行構建時遇到一些問題,我建議您在沒有緩存的情況下進行構建。 只需添加--no-cache
以確保確實調用了npm install
。
docker build --no-cache -t mybuild:v1 .
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.