Angular 6.0.3在docker中不起作用,因为它无法找到module @ angular-devkit / build-angular

时间:2018-05-23 10:27:12

标签: angular docker angular6

我尝试在docker-compose环境中添加angular6应用。我在本地创建了项目并且它可以正常工作,但在docker-compose builddocker-compose up之后,它会发送下一个错误:

angular6@0.0.0 start /usr/src/app
> ng serve --host 0.0.0.0 --disable-host-check -p 3000

Could not find module "@angular-devkit/build-angular" from "/usr/src/app".
Error: Could not find module "@angular-devkit/build-angular" from "/usr/src/app".
    at Object.resolve (/usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/core/node/resolve.js:141:11)
    at Observable.rxjs_1.Observable [as _subscribe] (/usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/architect/src/architect.js:132:40)
    at Observable.subscribe (/usr/local/lib/node_modules/@angular/cli/node_modules/rxjs/internal/Observable.js:162:69)
    at DoOperator.call (/usr/local/lib/node_modules/@angular/cli/node_modules/rxjs/internal/operators/tap.js:71:23)
    at Observable.subscribe (/usr/local/lib/node_modules/@angular/cli/node_modules/rxjs/internal/Observable.js:159:22)
    at /usr/local/lib/node_modules/@angular/cli/node_modules/rxjs/internal/util/subscribeTo.js:22:31
    at Object.subscribeToResult (/usr/local/lib/node_modules/@angular/cli/node_modules/rxjs/internal/util/subscribeToResult.js:7:45)
    at MergeMapSubscriber._innerSub (/usr/local/lib/node_modules/@angular/cli/node_modules/rxjs/internal/operators/mergeMap.js:132:38)
    at MergeMapSubscriber._tryNext (/usr/local/lib/node_modules/@angular/cli/node_modules/rxjs/internal/operators/mergeMap.js:129:14)
    at MergeMapSubscriber._next (/usr/local/lib/node_modules/@angular/cli/node_modules/rxjs/internal/operators/mergeMap.js:112:18)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! angular6@0.0.0 start: `ng serve --host 0.0.0.0 --disable-host-check -p 3000`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the angular6@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
angular-ayaresa    |
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2018-05-23T10_03_17_591Z-debug.log

这是我的Angular Dockerfile:

# Create image based on the official Node 8.10.0 image from dockerhub
FROM node:8.10.0
# Create a directory where our app will be placed
RUN mkdir -p /usr/src/app

# Change directory so that our commands run inside this new directory
WORKDIR /usr/src/app

# Copy dependency definitions
COPY ./quickstart /usr/src/app

# Install dependecies
RUN rm -rf node_modules/

RUN npm i --save

RUN npm i npm@latest -g
RUN npm install -g @angular/cli

# Expose the port the app runs in
EXPOSE 4200

# Serve the app
CMD ["npm", "start"]

模块@angular-devkit/build-angular已正确安装并包含在package.json

2 个答案:

答案 0 :(得分:1)

您的docker文件中有错误 您做了一个 WORKDIR ,并将其设置为 / usr / src / app

这意味着上下文将切换到该文件。

因此,当您执行this.service.get_items().subscribe((res) => { // request data will be here in the res }); 时,应为COPY ./quickstart /usr/src/app

码头工人文件说

COPY ./quickstart .

ref:https://docs.docker.com/engine/reference/builder/#copy

答案 1 :(得分:0)

我发现了问题。

我在docker-compose.yml中删除了一行。

angular-ayaresa:
    build:
      context: build/angular
    ports:
          - "4200:4200"
    volumes:
      - ../source/angular:/usr/src/app/
      - /usr/src/app/node_modules/ // <-- this line
    container_name: angular-ayaresa
    networks:
      - ayaresa-network

此行允许容器忽略卷中的文件夹。

问题是node_modules包含在卷中,Angular从本地"@angular-devkit/build-angular"而不是容器node_modules中搜索模块node_modules

我只添加了忽略node_modules,问题就解决了。