Search code examples
loggingnpmaureliapackage.json

How to ensure single version of dependency in consuming application


I'm using peerDependencies for this purpose, but wonder is it sufficient or not, or it is just not possible.

Using aurelia-logging, as an example.

I created a library, library-a, which uses aurelia-logging to do some logging.

For normal dependencies, I would just add it as dependencies. However, aurelia-logging contain some global state (i.e. storing all loggers created) so that it exposes a setLevel() function which can set the logLevel of all loggers.

Due to this global state, the end application needs to use a single version of aurelia-logging for the whole thing to work. If not, some modules may use a different version (thus a different instance) of aurelia-logging and the setLevel() function does not work correctly.

As a library author, I'm listing aurelia-logging as a dependencies as well as a peerDependencies.

Would it do what I wanted it to do?

Global state is general is a bad idea, but in this case, there seems to be no other way out of it.


Solution

  • As I think more about it, the solution is to have a version locked module to store these "global states".

    DISCLAIMER: You should avoid using global states as much as possible.

    I have created https://github.com/unional/global-store for this purpose.

    DISCLAIMER AGAIN: Only use it if you know that you are doing the right thing. Read the README. :)