Clean AND practical way to handle node_modules in a Dockerized Node.js dev environment?
I would suggest to look at this project https://github.com/BretFisher/node-docker-good-defaults, it supports both local and container node_modules by using a trick, but is not compatible with some frameworks (eg strapi):
- The
container node_modules
are placed one folder level up from the app (node's algo to resolve deps, recursively looks for up the folder of the app for the node_modules), and they are removed from the app folder - The
host node_modules
are placed inside the app folder (no changes there) - You extra
bind-mount the package.json + lock
files specifically between the container (remember up one folder from app) to host (app folder) so they are always kept in sync => So when youdocker exec npm install dep
, you only need tonpm install dep
on host and eventually rebuild your docker image.