I'm using Docker volumes, and now I get 'cannot find module express'. How can volumes cause this issue?

Question

Grade: Education Subject: Support
I'm using Docker volumes, and now I get 'cannot find module express'. How can volumes cause this issue?
Asked by:
103 Viewed 103 Answers

Answer (103)

Best Answer
(670)
When using Docker volumes, especially bind mounts, you can inadvertently override the container's `node_modules` directory. If you mount your local project directory into the container (e.g., `-v /path/to/my/app:/app`), and your local `node_modules` directory is empty, missing, or created for a different OS/architecture, it will effectively replace the `node_modules` that was built inside the container. To fix this, you can either ensure `node_modules` is correctly present on your host machine before mounting, or specifically exclude `node_modules` from the bind mount by mounting an anonymous volume over it (e.g., `-v /path/to/my/app:/app -v /app/node_modules`).