Missing node_modules folder in mounted volume after successful npm install in Docker container

My Setup

I’m working on a project with these components:

  • frontend/ - runs a Python Flask app on port 5000 with SQLite database
  • processor/ - contains app.js file that processes tasks from a queue. The Flask app communicates with this through JSON API on port 8080. This processor uses Redis and saves files to processor/data/

This issue is specifically about the processor service.

processor/Dockerfile

FROM node:0.12

WORKDIR /app

COPY package.json /app/
RUN npm install

COPY . /app/

docker-compose.yml

redis:
    image: redis
processor:
    build: ./processor
    command: npm start
    ports:
        - "8080:8080"
    volumes:
        - processor/:/app/
    links:
        - redis

The Problem

When I execute docker-compose build, everything works fine and npm packages get installed in /app/node_modules.

But running docker-compose up gives me this error:

processor_1 | Error: Cannot find module 'lodash'
processor_1 |     at Function.Module._resolveFilename (module.js:336:15)
processor_1 |     at Function.Module._load (module.js:278:25)

The node_modules directory is completely empty when the container starts. If I manually run npm install on my host machine, it works, but I want Docker to handle all dependencies.

What am I doing wrong? All required packages are listed in my package.json file.

Your volume mapping is wiping out the entire /app directory at runtime, including the node_modules that got installed during the build. I hit this exact issue last month with a React app. Instead of mounting the whole processor directory, just mount the specific files you need to change during development: yaml volumes: - ./processor/app.js:/app/app.js - ./processor/package.json:/app/package.json - ./processor/data:/app/data This keeps your node_modules intact while letting you edit your code. You’ll need to map each file or directory individually, but you get precise control over what syncs.

Yeah, classic Docker volume issue. Your host folder’s masking the container’s node_modules at runtime, even though the build worked fine. Add node_modules to a .dockerignore file in your processor directory - stops it from getting copied over from the host during mounting.

Your volume mount is overriding the node_modules directory that Docker creates during the build. When you mount processor/:/app/, it replaces everything in /app with your local processor/ folder - which doesn’t have the installed dependencies.

I hit this same issue last year with a Node app. Fix it by changing your docker-compose.yml volumes section:

volumes:
    - processor/:/app/
    - /app/node_modules

That second line creates an anonymous volume for node_modules so your host directory can’t overwrite it. You’ll keep all the dependencies from the build but still see your code changes in the container.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.