Wednesday, 10 May 2017

Specifying shared npm packages in parent directory

I have a git repo which contains a few Node.js powered websites; for simplicity of dependency management, all required production npm modules are committed to the repo. To save space and avoid duplication, I'd like to save many of the shared modules in a parent node_modules directory that each site (sub directory) can implicitly or explicitly use.

Example:

+
|- Site1
|  |- node_modules (modules unique to Site1, e.g. module1...)
|
|- Site2
|  |- node_modules (modules unique to Site2, e.g. module2...)
|
|- Site3
|  |- node_modules (modules unique to Site3, e.g. module3...)
|
|- node_modules (shared/common modules)
   |- shared1@2
   |- shared2@4
   |- etc.

The package.json file in each directory (Site1, Site2, Site3) references only the modules installed within that directory (not the shared ones that are used).

Imagine module1 used by Site 1 requires shared1@1, while the shared version is shared1@2.

With npm@1 and npm@2, this works fine as the dependencies for module1 are installed in its own node_modules subdirectory (Site1\node_modules\module1\node_modules\...).

With npm@3 and the flat hierarchy, we start to run into some problems. shared1@1 is installed to Site1\node_modules\shared1, and as a result Site1 will use that version instead of the 'correct' shared copy at node_modules\shared1.

What are the options for addressing this? Is npm link viable? Or should I give up, and just duplicate the modules in each sub-directory?



via Dan

No comments:

Post a Comment