How to fork & patch npm modules
“With now more than 3000 modules, there are huge gaps in the quality of things you find in the npm registry. But more often than not, it’s easy to find a module that is really close to what you need, except if it wasn’t for that one bug or missing feature.” – Felix Geisendörfer
Today I read “How to fork & patch npm modules” from Debuggable by Felix Geisendörfer.
Npm is one of my favorite package managers, mostly because of the way it handles dependencies – each library is allowed to use its preferred version of a dependency. This works great when you want to fork an existing package as shown in the linked blog post. It’s really easy to modify where your dependencies are located. See how Felix customized his package.json
to refer to a forked version of the xmlbuilder
dependency in his own GitHub repository instead of the original npm package:
"dependencies": {
"request": "1.9.8",
"xmlbuilder": "https://github.com/felixge/xmlbuilder-js/tarball/4303eb2650a4b819a980b1dc9d2965862a1e9faf",
"stack-trace": "0.0.5",
"traverse": "0.4.4",
"hashish": "0.0.4"
},
You just substitute the GitHub tarball URL (the final segment of the URL is just the SHA hash for the commit, of course) in the place of the previous npm version string. Next, create a pull request on GitHub and submit your changes upstream (if applicable)!