Angular 8 Docker容器即使已安装也无法找到模块

时间:2019-09-18 19:53:45

标签: angular docker npm

我正在构建Angular 8 SPA。我有一个使用Docker Compose配置构建并运行的Dockerfile。

该配置已经运行了几周,但是今天,它突然停止了工作。我没有对以前的工作进行任何更改。

由于模块错误,Angular应用程序不再编译,并且由于此问题,现在所有开发都被阻止。

error TS2307: Cannot find module 'ngx-cookie-service'.

我尝试过:

  • 卸载并重新安装ngx-cookie-service模块

  • 删除并重新创建node_modules和package-lock.json

  • 运行npm缓存清理

  • 运行docker系统修剪并重新构建映像

  • 手动更改ngx-cookie-service的版本

  • 确认存在node_modules / ngx-cookie-service目录

问题:

我还可以采取什么其他步骤来解决此问题?

package.json

{
  "name": "client",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test --browsers ChromeHeadlessNoSandbox",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~8.0.0",
    "@angular/cdk": "^8.0.1",
    "@angular/common": "~8.0.0",
    "@angular/compiler": "~8.0.0",
    "@angular/core": "~8.0.0",
    "@angular/flex-layout": "^8.0.0-beta.26",
    "@angular/forms": "~8.0.0",
    "@angular/material": "^8.0.1",
    "@angular/platform-browser": "~8.0.0",
    "@angular/platform-browser-dynamic": "~8.0.0",
    "@angular/router": "~8.0.0",
    "angular2-query-builder": "^0.4.2",
    "express-jwt": "^5.3.1",
    "file-saver": "^2.0.2",
    "hammerjs": "^2.0.8",
    "ngx-cookie-service": "^2.0.0",
    "rxjs": "~6.4.0",
    "tslib": "^1.9.0",
    "xlsx": "^0.15.1",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.800.0",
    "@angular/cli": "~8.0.1",
    "@angular/compiler-cli": "~8.0.0",
    "@angular/language-service": "~8.0.0",
    "@types/node": "~8.9.4",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "^5.0.0",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.4.3"
  }
}

Dockerfile

### Local development only ###

FROM node:12.8-alpine

# Install bash
RUN apk add --no-cache bash

# Create working directory
RUN mkdir -p /home/node/app

# Set working directory
WORKDIR /home/node/app

# Add `/home/node/app/node_modules/.bin` to $PATH
ENV PATH /home/node/app/node_modules/.bin:$PATH

# Copy code
COPY ./angular /home/node/app

# Expose ports
EXPOSE 4201:4201

RUN npm install

# Start Angular
CMD ng serve --host 0.0.0.0

docker-compose.yml

version: '3.5'

services:

  client:
    build:
      context: ./client
      dockerfile: Dockerfile
    volumes:
      - './client/angular/:/home/node/app'
      - '/home/node/app/node_modules'
    ports:
      - '4201:4201'
    depends_on:
      - server

2 个答案:

答案 0 :(得分:1)

在构建期间将构建复制到Dockerfile中时,则无需将主机目录挂载到docker-compose中。

# Copy code
COPY ./angular /home/node/app

这足以运行该应用程序,因此您可以将它们从Docker-compose中删除。

      - './client/angular/:/home/node/app'
      - '/home/node/app/node_modules'

如果要运行表单容器,请使用容器挂载构建目录。

      - './client/angular/:/home/node/app'

此外,您可以调试软件包。安装后,列出软件包。

RUN npm install
RUN npm list

或者,如果仍然存在问题,则尝试将该程序包作为全局程序包。

RUN npm i ngx-cookie-service -g

有时由于不同的二进制文件,主机绑定模块无法正常工作,因此最好在运行时将其安装在docker-compose中。

将此添加到docker-compose文件中

command: sh -c "
  npm i &&
  ng serve --host 0.0.0.0"

答案 1 :(得分:0)

在完成 @Alli 接受的答案后,我遇到了错误 Cannot find module '@angular-devkit/build-angular/package.json',我通过告诉我的 Dockerfile 像这样安装它来修复它:

RUN npm install -g @angular/cli
RUN npm install --save-dev @angular-devkit/build-angular
RUN npm install

对于那些出于开发目的而 Dockerizing Angular 的人,不要删除卷定义,这样代码中的任何更改都会反映在容器中。

不要删除开发环境的卷定义

      - './client/angular/:/home/node/app'
      - '/home/node/app/node_modules'