Jivan
Jivan

Reputation: 23068

node_modules missing even after npm install in docker container

The Dockerfile being used:

FROM node:8-alpine

WORKDIR /usr/src/app
COPY . .
RUN npm install

CMD ["npm", "run", "serve"]

EXPOSE 8080

And the docker-compose.yml file:

version: '3'

services:
    app:
        container_name: app
        restart: always
        build:
            context: ./app
            dockerfile: Dockerfile
        ports:
            - "8080:8080"
        volumes:
            - ./app:/usr/src/app
            - ./logs:/logs

The folder structure is the following:

project/
|-- docker-compose.yml
|-- logs/
|-- app/
    |-- Dockerfile
    |-- package.json

When running docker-compose up --build from project/, the npm install step outputs the following after about one minute:

added 1684 packages from 1297 contributors and audited 36429 packages in 56.23s
found 0 vulnerabilities

However, at the npm run serve step, the output basically consists in saying that no npm module can be found, and among other things, this line:

npm WARN Local package.json exists, but node_modules missing, did you mean to install?

How comes npm install is actually and definitely executed, but npm complains that node_modules cannot be found?

Upvotes: 15

Views: 16474

Answers (2)

Roman Malkevych
Roman Malkevych

Reputation: 290

I had the same problem and I solved it just following this instruction. Add one line of code - /usr/src/app/node_modules to the docker-compose.yml file in the volumes:

volumes:
  - ${PWD-.}/name_of_your_app:/usr/src/app
  - /usr/src/app/node_modules

Upvotes: 10

Jivan
Jivan

Reputation: 23068

Update: I just ended up using only ./app/src folder as a volume, instead of ./app.

This way, /app/node_modules is not overridden by the host's volume.

version: '3'

services:
    app:
        container_name: app
        restart: always
        build:
            context: ./app
            dockerfile: Dockerfile-dev
        ports:
            - "8080:8080"
        volumes:
            - ./app/src:/usr/src/app/src # <---- this
            - ./logs:/logs

Upvotes: 5

Related Questions