Getting Docker, Node and NPM to play nice

Docker is an incredible tool for managing both production workflows as well as development workflows. Unfortunately, the most common pattern for doing development work using docker (mapping your source code folder as a volume), wreaks havoc on Node.

Node and NPM, by default, install all of the dependencies in the node_modules directory in the root directory of your project. When you, for development purposes, share that directory between your VM and your main computer’s file system you can wind up with a ton of permissions errors and failed dependency installations or lookups.

It doesn’t have to be this way! Though it took me a while to figure out the right incantations and settings to make it all work, the resulting thing was simple. You can set a “installation prefix” when you use npm to install dependencies, and you can set a NODE_PATH to include that prefixed installation directory. This way, your dependencies live in a directory that isn’t shared between your desktop and docker environments, and you can easily develop your node projects using docker and docker-compose to manage your environments.