skip to Main Content

I’m experiencing an issue where Docker doesn’t seem to update the node_modules folder after I modify dependencies in package.json. Even after rebuilding the Docker image and container, the application still throws errors related to the missing dependencies. The changes to node_modules only take effect if I completely remove the container and its associated volume before rebuilding.

Dockerfile:

FROM node:22-alpine

RUN npm install -g npm@latest

USER node

WORKDIR /app

COPY --chown=node:node package.json package-lock.json ./

RUN npm install

COPY . .

EXPOSE 5173

CMD ["npm", "run", "dev"]

docker-compose.yml

services:
  npm-test:
    build: .
    ports:
      - "5173:5173"
    volumes:
      - .:/app
      - node_modules_docker:/app/node_modules

volumes:
  node_modules_docker:

Steps to reproduce:

  1. Build and run the container (everything works):

    docker compose up --build
    
  2. Remove significant dependencies from package.json e.g. @vitejs/plugin-react

  3. Run npm install to update package-lock.json

  4. Stop and remove the container, images, and volumes, then rebuild (use the following commands to delete everything, or replace IDs to target specific ones):

    docker stop $(docker ps -q)
    docker rm $(docker ps -aq)
    docker rmi $(docker images -q)
    docker volume rm $(docker volume ls -q)
    
    docker compose up --build
    
  5. The following error will show up:

    Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@vitejs/plugin-react' imported from /app/node_modules/.vite-temp/vite.config.ts.timestamp-1733299382139-6e1c72bf771df.mjs

  6. Add back the missing dependencies to package.json and run npm install

  7. Build and run the container again:

    docker compose up --build
    
  8. The app should work again (it is "the same" as in the beginning), but the error persists

Repository with MRE

2

Answers


  1. My guess would be that you’re not deleting all the volumes and images. I faced the same issue with Vue, and btw it actually should work when you rebuild, but it wasn’t. So I just deleted the image itself and volumes. If you type docker system prune -af --volumes there were still volumes present, the command doesn’t entirely remove all images, volumes and containers. I had to type docker volume prune -a for all the volumes to be removed. So you can try delete everything as you tried before, but before building again, just type docker volume ls and docker image ls to be sure.

    One more point:
    You’re copying everything into the container with copy . . and mounting your root
    volumes:
    – .:/app
    My assumption is your project dir represented by the . (node_modules included). The problem is, docker will pick the node_modules you have locally and replace the node_modules inside the container when you fire up the container. To prove this, try install the dependency locally without docker, then try running the container again, chances are you won’t see the error. When you try to run the container, and you’re copying he node_modules, that means node will use the dependencies there, inside the node_modules we’ve copied (the one added missing). I hope that helps.

    Login or Signup to reply.
  2. The first time you run the container, the contents of /app/node_modules in the image is copied into the node_modules_docker volume because the volume doesn’t exist.

    When you run it a second time the volume isn’t empty, so nothing is copied.

    This behavior is documented here.

    To make sure that the data is always copied, you need to delete the volume. You can do that by deleting it using

    docker volume rm <volume name>
    

    or bring the system down with the -v option which tells compose to delete all named volumes in the compose file

    docker compose down -v
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search