Think twice, please!
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Node Dependency Clashing

This Example

This is a simple example of how extending a native prototype (String.prototype.yourMethod, Array.prototype.yourMethod, etc) can not only interfere with other modules that made the same methods, but even with yourself, when an app relies on multiple versions of your module (by way of its dependencies).


The dependency tree for this app looks like:

├── proto-extender@2.0.0
└─┬ dep@1.0.0
  └── proto-extender@1.0.0

So, in words, our app needs proto-extender version 2, but we have a dependency, dep, that needs proto-extender version 1.

proto-extender is a module that extends a native prototype:

Array.prototype['proto-extender'] = function () { /* ... */ };

Between proto-extender version 1 and 2, the API has changed. So, what happens when your app requires both versions? Nothing good!

To cause an error (and then fix it), these lines from index.js can be re-ordered:


Reading Material

Note: this was just a quick way for me to explain how prototypes can clash in a Node app. If you can extend this example, add an entirely new one, or link me to a great article or example elsewhere, I'll be happy to point this repo to that resource. Thanks!