Zachary Oldham
Zachary Oldham

Reputation: 868

Version of dependency vs. version of dependency of dependency

Let's say that I have dependency X version 1.0 and dependency Y version 1.0 in package.json. If Y requires X version 2.0 (which I know because I looked in package-lock.json), will I still be able to use X version 1.0 in my code without issues?

Upvotes: 0

Views: 30

Answers (1)

jfriend00
jfriend00

Reputation: 707238

With a couple of assumptions about good module behavior, it is perfectly feasible for two modules of differing versions to be in use in the same app.

Here are some of the things a "good behavior" module must do in order to allow this:

  1. Not use global symbols that would conflict between versions (stick to exports only). If everything is done via module exports, then two versions of the same module can run just fine. Each one will be separately imported and the code will use only the imports from the appropriate version.
  2. Have the two versions installed in different directories or with different names. The idea here is that the path to the module must be different between the two because that's how module caching works. If the full filename is different (either because of a differing install path or a differing filename), then the module loader will happily load each one separately.
  3. Not register anything globally that would conflict (like not both try to start a server on the same port).
  4. Not try to write data to the same file. If all file activity is relative to the install directory for the module, then this should be safe. But, if the module assumes something about a known path or both are using a path from the same environment variables and they end up conflicting on writing data to the same file, that could cause problems.
  5. Not try to write conflicting property names to the same object. For example if both versions were in action as Express middleware and both were trying to write different things to the req.someProp property that could cause problems. But, if both versions weren't in use on the same requests or both were being used for different functionality, then this could work just fine.

will I still be able to use X version 1.0 in my code without issues?

So, it's certainly possible, but it depends upon the behavior of the module and what exactly it does globally or with shared resources.

Upvotes: 1

Related Questions