Node modules not found with Dockerfile and docker-compose due to bind mount Node modules not found with Dockerfile and docker-compose due to bind mount docker docker

Node modules not found with Dockerfile and docker-compose due to bind mount


When you mount the volume of your project into the container working directory your overriding any changes you've made since the original copy/add in your Dockerfile with what is in your local project directory. In this case that means the node_modules folder. The easiest way I've found to fix this problem is to not mount the entire project directory, but just a subfolder that has my source code.


You need to either modify your project structure moving every editable code inside a folder let's say src, or update your Dockerfile to move node_modules a directory upper.

As we know, when Node can't find node_modules in the current directory, it starts looking for it in the parent directory, so we take advantage of it.

a Dockerfile similar to this will solve your problem.

FROM node:10.16-alpineWORKDIR /nodeCOPY package.json package-lock.json ./RUN npm installWORKDIR /node/appCOPY . .CMD ["npm", "start"]

Please make sure to make appropriate changes to the docker-compose.yml too (directory structure has changed)