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:
-
Build and run the container (everything works):
docker compose up --build
-
Remove significant dependencies from
package.json
e.g.@vitejs/plugin-react
-
Run
npm install
to updatepackage-lock.json
-
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
-
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
-
Add back the missing dependencies to
package.json
and runnpm install
-
Build and run the container again:
docker compose up --build
-
The app should work again (it is "the same" as in the beginning), but the error persists
2
Answers
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 typedocker 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 typedocker volume ls
anddocker image ls
to be sure.One more point:
You’re copying everything into the container with
copy . .
and mounting your rootvolumes:
– .:/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.
The first time you run the container, the contents of
/app/node_modules
in the image is copied into thenode_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
or bring the system down with the
-v
option which tells compose to delete all named volumes in the compose file