Specifying shared npm packages in parent directory

Dan

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? I'm not sure npm link is viable, since it links via the local global directory (which isn't committed to git). Or should I give up, and just duplicate the modules in each sub-directory?

Dan

I found a workaround that seems to do what I want. Basically, for any module which is installed in the common node_modules directory, it is also listed in the site-specific sub-directory's package.json as a dependency. However, I don't actually install the package to the subdirectory.

The presence of the package in the sub-directory's manifest is sufficient for npm to avoid installing that package, or alternate versions of that package, in the root of the sub-directory's node_modules.

There are some limitations, such as tools (e.g. npm-check) thinking dependencies are missing, and not being able to do a full npm i in the sub-directory, but they're all easy to overcome. Hopefully this helps someone else in the same boat!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

npm install from the parent directory

From Dev

Global npm packages are install on different user directory

From Dev

npm installs packages and dependencies in the project root directory

From Dev

Listing and specifying packages in debootstrap

From Dev

Composer specifying 'in development' packages?

From Dev

change npm install path for global packages to a custom directory

From Dev

Transparently install npm packages offline, from a custom filesystem directory

From Dev

How can get the file directory that require my npm packages

From Dev

Installing npm modules in a VM shared directory and grunt issues

From Dev

How can I change directory into any a folder two directories below without specifying parent directories?

From Dev

Meteor Npm.require() cannot find a file in parent directory

From Dev

Meteor Npm.require() cannot find a file in parent directory

From Dev

Run nested npm script from parent directory in Netlify?

From Dev

Why does sbt-native-packager generate a hardcoded top-level parent directory in distribution packages?

From Dev

Specifying desired packages for Gradle Javadoc task

From Dev

Specifying desired packages for Gradle Javadoc task

From Dev

Use npm in Meteor packages

From Dev

manually install npm packages?

From Dev

npm update packages recursively

From Dev

Working with npm packages

From Dev

Npm does not install packages

From Dev

Install npm packages in Titanium

From Dev

Import npm packages

From Dev

GCC: Specifying a Minimum Shared Library Version

From Dev

NPM dependencies shared by dependencies

From Dev

NPM dependencies shared by dependencies

From Dev

Node.js require() does not look in parent directory when using "npm link"

From Dev

Private NPM modules installed via package.json without a parent directory?

From Dev

Installing packages into local directory?

Related Related

  1. 1

    npm install from the parent directory

  2. 2

    Global npm packages are install on different user directory

  3. 3

    npm installs packages and dependencies in the project root directory

  4. 4

    Listing and specifying packages in debootstrap

  5. 5

    Composer specifying 'in development' packages?

  6. 6

    change npm install path for global packages to a custom directory

  7. 7

    Transparently install npm packages offline, from a custom filesystem directory

  8. 8

    How can get the file directory that require my npm packages

  9. 9

    Installing npm modules in a VM shared directory and grunt issues

  10. 10

    How can I change directory into any a folder two directories below without specifying parent directories?

  11. 11

    Meteor Npm.require() cannot find a file in parent directory

  12. 12

    Meteor Npm.require() cannot find a file in parent directory

  13. 13

    Run nested npm script from parent directory in Netlify?

  14. 14

    Why does sbt-native-packager generate a hardcoded top-level parent directory in distribution packages?

  15. 15

    Specifying desired packages for Gradle Javadoc task

  16. 16

    Specifying desired packages for Gradle Javadoc task

  17. 17

    Use npm in Meteor packages

  18. 18

    manually install npm packages?

  19. 19

    npm update packages recursively

  20. 20

    Working with npm packages

  21. 21

    Npm does not install packages

  22. 22

    Install npm packages in Titanium

  23. 23

    Import npm packages

  24. 24

    GCC: Specifying a Minimum Shared Library Version

  25. 25

    NPM dependencies shared by dependencies

  26. 26

    NPM dependencies shared by dependencies

  27. 27

    Node.js require() does not look in parent directory when using "npm link"

  28. 28

    Private NPM modules installed via package.json without a parent directory?

  29. 29

    Installing packages into local directory?

HotTag

Archive