0

I found many answers online to the exception Cannot find module '@angular-devkit/build-angular/package.json, among which adding @angular-devkit/build-angular@0.1102.9 --force , but none worked in my case.

Problem

Running an Angular application using docker-compose: after building the application, the error below occurs when running docker-compose up; note, the app compiles successfully when running docker run <image>, but I need it to work with Docker Compose.

defuse-ui         | An unhandled exception occurred: Cannot find module '@angular-devkit/build-angular/package.json'
defuse-ui         | Require stack:
defuse-ui         | - /usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/architect/node/node-modules-architect-host.js
defuse-ui         | - /usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/architect/node/index.js
defuse-ui         | - /usr/local/lib/node_modules/@angular/cli/models/architect-command.js
defuse-ui         | - /usr/local/lib/node_modules/@angular/cli/commands/serve-impl.js
defuse-ui         | - /usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/schematics/tools/export-ref.js
defuse-ui         | - /usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/schematics/tools/index.js
defuse-ui         | - /usr/local/lib/node_modules/@angular/cli/utilities/json-schema.js
defuse-ui         | - /usr/local/lib/node_modules/@angular/cli/models/command-runner.js
defuse-ui         | - /usr/local/lib/node_modules/@angular/cli/lib/cli/index.js
defuse-ui         | - /usr/local/lib/node_modules/@angular/cli/lib/init.js
defuse-ui         | - /usr/local/lib/node_modules/@angular/cli/bin/ng
defuse-ui         | See "/tmp/ng-nVUdCb/angular-errors.log" for further details.
defuse-ui exited with code 127

Dockerfile

FROM node:14.16.1

WORKDIR /app

ENV PATH /app/node_modules/.bin:$PATH

COPY package.json /app/package.json

RUN npm install -g @angular/cli@11.2.9 @angular-devkit/build-angular@0.1102.9 --force
RUN npm install

COPY . /app

CMD ng serve --host 0.0.0.0 --port 4200

package.json, package-lock.json and other files are accessible at https://github.com/radon-h2020/radon-defuse/tree/905d7a339a31bc68dbd9b7a5258b5e9b19e65c68 release 1.0.0

Versions:

  • Docker version 20.10.17, build 100c701
  • docker-compose version 1.29.2, build unknown
s.dallapalma
  • 1,225
  • 1
  • 12
  • 35

3 Answers3

1

After days of trials, I finally found a solution on this useful webpage. The docker-compose.yml has to be updated with a new volume pointing to the node_modules:

services:
  defuse-ui:
    build: ./defuse
    container_name: defuse-ui
    ports:
      - 4200:4200
    expose:
      - 4200
    volumes:
      - /app/node_modules  # <-- Add this
      - ./defuse:/app # <!-- or remove this

A possible explanation, as described in the webpage is that

when docker builds the image, the node_modules directory is created within the worker directory, and all the dependencies are installed there. Then on runtime the working directory from outside docker is mounted into the docker instance (which does not have the installed node_modules), hiding the node_modules you just installed. A workaround is to use a data volume to store all the node_modules, as data volumes copy in the data from the built docker image before the worker directory is mounted

General Grievance
  • 4,555
  • 31
  • 31
  • 45
s.dallapalma
  • 1,225
  • 1
  • 12
  • 35
-1

2 possible solutions:

  1. Uninstall/remove node_modules and install again:

    npm install -g @angular/cli@11

    npm install @angular-devkit/build-angular@0.1102.9

  2. Remove this line in docker-compose.yml:

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

Pranam Bhat
  • 42
  • 10
  • Regarding solution 1: the node_modules is not present in the container when is being built, therefore no need to remove it. Then, it already installs angular/cli and dev-kit. As for solution 2, it seems to be copied-pasted from another solution on StackOverflow, since my docker-compose.yml does not have such code – s.dallapalma Jul 23 '22 at 08:42
  • Can you give your package.json? Run: npm update -g @angular/cli – Pranam Bhat Jul 23 '22 at 13:09
  • It should work! 1. npm install --save-dev @angular-devkit/build-angular 2. npm rm -rf node_modules 3. Delete your package-lock.json 4. npm cache clean --force 5. npm install --save-dev @angular/cli@11.2.9 – Pranam Bhat Jul 23 '22 at 13:16
  • 1
    The package.json and all related files are at the link I provided in the response. It's an open-source project – s.dallapalma Jul 24 '22 at 10:03
-1

In package.json:

Instead of

"@angular/cli": "1.6.0",

change it to

"@angular/cli": "^1.6.0",

Or, in front of particular version of your local machine, add "^".

Then run this command:

npm update

npm update -g @angular/cli
Pranam Bhat
  • 42
  • 10