Node modules missing from mounted volume after successful npm install in Docker container

I’m having trouble with my Docker setup where npm packages get installed during the build process but disappear when I mount a volume.

My project structure includes the following:

  • backend/ - This is a Python Flask app that runs on port 5000 and uses SQLite.
  • processor/ - This is a Node.js service that has an app.js file to handle queue processing, communicates through a JSON API on port 8080, utilizes Redis for caching, and saves files in the processor/assets/ directory.

This question is specifically about the processor service.

processor/Dockerfile:

FROM node:10

WORKDIR /app

COPY package.json /app/
RUN npm install

COPY . /app/

docker-compose.yml:

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

Upon running docker-compose build, everything works as it should and all dependencies are installed in /app/node_modules as expected. I can confirm this as I see the installation output:

npm WARN package.json [email protected] No description

> [email protected] install /app/node_modules/html-pdf/node_modules/puppeteer
> node install.js

<output continues>

However, when I run docker-compose up, I encounter a module error:

processor_1 | Error: Cannot find module 'lodash'
processor_1 |     at Function.Module._resolveFilename (module.js:548:15)
processor_1 |     at Function.Module._load (module.js:475:25)
processor_1 |     at Module.require (module.js:597:17)
processor_1 |     at require (module.js:613:17)
processor_1 |     at Object.<anonymous> (/app/app.js:2:18)
processor_1 |     at Module._compile (module.js:649:30)
processor_1 |     at Object.Module._extensions..js (module.js:660:10)
processor_1 |     at Module.load (module.js:565:32)
processor_1 |     at tryModuleLoad (module.js:505:12)
processor_1 |     at Function.Module._load (module.js:497:3)

It appears that the /app/node_modules folder is completely empty in both the Docker container and on the host machine.

When I run npm install on my host device manually, everything then works well. However, I would prefer that Docker handle all dependencies without requiring me to perform local installations.

What could be causing this issue? All necessary packages are accurately listed in my package.json file.

Your volume mapping is wiping out the /app/ directory, including the node_modules that Docker built. When you mount ./processor/:/app/, it replaces everything in the container with your local folder - but your local folder doesn’t have node_modules since you haven’t run npm install there. Fix it by excluding node_modules with an anonymous volume, or restructure your Dockerfile to install dependencies after copying files. You could also just bind mount specific subdirectories instead of the whole project root.

Classic volume mount issue - your ./processor/:/app/ volume’s overriding the node_modules installed during build. Try adding a named volume for node_modules or move the COPY before npm install so it doesn’t get wiped.

I encountered a similar issue when working with a React app. The problem lies in how Docker Compose handles volumes: when you mount ./processor/:/app/, it completely replaces the contents of /app/ with what’s in your local folder, which does not include node_modules. To solve this, you can either add node_modules:/app/node_modules as an anonymous volume in your docker-compose.yml to preserve that directory or modify your Dockerfile to copy all necessary files before running npm install. I recommend using the anonymous volume approach to maintain your current setup while preventing the loss of node_modules.