Search code examples
npmnode-modulespackage.json

Common node_modules directory for all projects


So I've been coding for a while, and have created a bunch of projects. Each of them has its own node_modules directory and it takes so much space. Is it a good practice to set one global directory for all projects, or should every single one of them have their own? And if there should be one for all, what is the proper way to do this? How do I connect it to all projects?


Solution

  • It’s very bad practice to install packages to a global node_modules, since different versions of them will conflict.

    A more efficient approach in terms of space that preserves the benefits of individual node_modules is to have a central cache of version-differentiated packages that are then hard-linked to your projects, for example. Package managers like pnpm implement this approach.