Modifying npm packages - the right way (2024)

Modifying npm packages - the right way (1)

If you are a frontend developer, you may have come across the following situation. You find the perfect dependency for your app. It does everything you need and works well, except for one thing. One little bug, one small aspect of it behaves partially the way it should in your app, and there is no way to set it up right. It is almost perfect, but not quite.

This can be frustrating as you may have already gone ahead and written a lot of code specific to this dependency, which would most likely be wasted if the library is replaced. Also if the dependency was a perfect match in the first place, there might not be others which match your needs like this one, so you would most likely have to compromise.

Using npm as your package manager will save a lot of time and keep things simple. With some extra coding you might get to keep your codebase and the dependency. You may also get some credit from the package authors and contributors and increase your GitHub reputation and on the plus side you get the awesome feeling of fixing or extending something many others are using.

For this you would need the library to be open source, but this is typical for the greater part of the packages hosted on npm. GitHub was just an example as this is used by the vast majority of the package sources, but any other will do too.

  • To begin, clone the repo of the source of the package you would like to modify or fork it first and then clone the fork. The repo source is linked in the official npm page of the package most of the time. If the library version is somehow specific to your needs, make sure you select the appropriate commit after the clone.
  • Installing the package dependencies should be done by using the ‘npm install’ command issued in the same directory as the package.json file. Now you have to familiarise yourself with building the library. Usually there is a guide in the readme on how to do it, but if there isn’t or it is not up to date, checking the package.json for scripts will do. Note that building the library may need additional dependencies, tools or environment which you have to set up manually.
  • If you can build the library, the next step is to fire up your favorite IDE for this task and dig into the code. Be prepared for what you might find, though. I have seen libraries with great code, while it took me long to understand what is going on in others. The comments written in a different language other than English in the source code are usually not of great help either, especially if you do not speak that language. Usually there is a demo included which you may be able to use for testing purposes.
  • If you have completed all this, you have to replace the library with the custom modified one to test it in your app. You can overwrite the existing files in the node_modules directory, but that is problematic as the modification will only work on your computer.
  • Fortunately, npm is a great partner in this. You should copy the library somewhere in the directory structure of your project. I like using directories named ‘Dependencies’ for this purpose.
  • Open your app’s package.json file and find the row where your library is listed as a dependency or a devDependency.
  • Then replace the version number with the path of the modified library directory and a ‘file:’ prefix. For example: "some-great-library": "file:ClientApp/Dependencies/some-great-library". Note that the path must use forward slashes even in Windows and it is relative to the location of the package.json file. In my example, the package.json and the ClientApp directory are in the same directory.
  • Modifying npm packages - the right way (2)
  • Next it’s time to “install” the dependencies. Issue ‘npm install’ for this in the directory that contains your app’s package.json. If you like to make sure, you may delete the library directory from the node_modules directory first. After the npm install runs, you will notice that it creates a link in node_modules to your custom library location.
  • Modifying npm packages - the right way (3)
  • You may now build and test your app using the custom library. This solution is great because it only relies on how npm works. If your co-workers use npm and you commit the new package.json as well as the custom-built library, it will work for them as well. If your build server uses npm, it will work on that also out of the box.
  • After everything is functioning, you should commit your changes (that is, if you have forked the original repo) and create a pull request against the original repository. This way your changes are likely to make it to the official release and you might become a contributor.

One last important thing to note is to make sure to check the license of the library you would like to modify. There is a chance that your Pull Request will not be merged or the package is already abandoned, the author is not available, etc. and it is important to know if you are allowed to use a modified copy within your app.

Modifying npm packages - the right way (2024)
Top Articles
Maximizing Tax Deductions | Mom Life | Nashville Wifestyles
MYTE | MYT Netherlands Parent B.V. short interest and earnings date annual report Mar, 2024
Aiken County government, school officials promote penny tax in North Augusta
The Best Classes in WoW War Within - Best Class in 11.0.2 | Dving Guides
What Happened To Father Anthony Mary Ewtn
Geometry Escape Challenge A Answer Key
Toonily The Carry
4156303136
‘Accused: Guilty Or Innocent?’: A&E Delivering Up-Close Look At Lives Of Those Accused Of Brutal Crimes
Missed Connections Dayton Ohio
Echat Fr Review Pc Retailer In Qatar Prestige Pc Providers – Alpha Marine Group
Icommerce Agent
Shasta County Most Wanted 2022
CANNABIS ONLINE DISPENSARY Promo Code — $100 Off 2024
Candy Land Santa Ana
Accuweather Mold Count
Fort Mccoy Fire Map
Cbssports Rankings
Bella Bodhi [Model] - Bio, Height, Body Stats, Family, Career and Net Worth 
Miltank Gamepress
Talk To Me Showtimes Near Marcus Valley Grand Cinema
Bocca Richboro
Crossword Help - Find Missing Letters & Solve Clues
Scripchat Gratis
Wat is een hickmann?
Cars & Trucks - By Owner near Kissimmee, FL - craigslist
Gen 50 Kjv
Neteller Kasiinod
Little Einsteins Transcript
Free Tiktok Likes Compara Smm
Missing 2023 Showtimes Near Mjr Southgate
Kids and Adult Dinosaur Costume
Housing Assistance Rental Assistance Program RAP
Poster & 1600 Autocollants créatifs | Activité facile et ludique | Poppik Stickers
Ni Hao Kai Lan Rule 34
Junior / medior handhaver openbare ruimte (BOA) - Gemeente Leiden
Domina Scarlett Ct
20 bank M&A deals with the largest target asset volume in 2023
Who Is Responsible for Writing Obituaries After Death? | Pottstown Funeral Home & Crematory
Walmart Car Service Near Me
Mybiglots Net Associates
20 Mr. Miyagi Inspirational Quotes For Wisdom
Backpage New York | massage in New York, New York
3367164101
Wisconsin Volleyball titt*es
Mit diesen geheimen Codes verständigen sich Crew-Mitglieder
91 East Freeway Accident Today 2022
Minecraft Enchantment Calculator - calculattor.com
Craigslist.raleigh
Latest Posts
Article information

Author: Fredrick Kertzmann

Last Updated:

Views: 5677

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Fredrick Kertzmann

Birthday: 2000-04-29

Address: Apt. 203 613 Huels Gateway, Ralphtown, LA 40204

Phone: +2135150832870

Job: Regional Design Producer

Hobby: Nordic skating, Lacemaking, Mountain biking, Rowing, Gardening, Water sports, role-playing games

Introduction: My name is Fredrick Kertzmann, I am a gleaming, encouraging, inexpensive, thankful, tender, quaint, precious person who loves writing and wants to share my knowledge and understanding with you.