Docker Tip #75: How to Avoid node_modules in Your Volume Mounts
Volume mounts are great in development, but it's usually not great when you end up with node_modules on your Docker host.
node_modules directory happens to have gotten volume mounted. This would typically happen if you were volume mounting
.:/app in development.
If you’re using yarn, you can prevent that from happening very easily without having to resort to using hacks.
1. Create a
.yarnrc file in the same location as your
Then add this line to the file:
By doing that, your node modules will get installed to
/node_modules instead of relative to your
package.json file which means they won’t be volume mounted back to your dev box if you do a volume mount such as
2. Adjust your
You’re likely copying in your
package.json file before you copy in all of your code to leverage Docker’s ability to cache layers.
Adjust that COPY line to be:
COPY package.json *yarn* ./
yarn.lock) will be copied along with your
3. Update any script paths:
With your node modules living in
/node_modules, you may need to update certain script paths if you reference them, such as using
Now you would reference
/node_modules/webpack/bin/webpack.js --mode=production -p or whatever script you’re planning to run.
This same strategy applies to other language package managers that install dependencies to relative folders by default (such as Mix with Elixir or Composer with PHP).