How to fork GitHub repository and use as npm dependency inside a react-native project
Pradnyanand Milind Pohare
Posted on February 27, 2020
Forking is the one thing I have never used in git. Yesterday I came to know how can we use it.
Why we use a fork?
Open source is a great thing, someone writes a library, so that others can use it, others can add up to it. Let's assume you are using an open-source library in your project, the library has everything you need except that the one thing you need for your project. That's where forking comes to help. Basically forking means you are creating a copy of the open-source library, making appropriate changes, committing back or submitting a pull request to the author of the open-source library.
How to fork an open-source library
Click on the Fork button on the right top corner of open-source GitHub repository
Then clone the forked library inside your local directory
git clone "forked lib URL"
Make appropriate changes to the library and commit it
git add .
git commit -m “commit message”
git push origin master
Now all the changes you want to make in the open-source library are there, the only thing remaining is to use this forked library as a dependency in your react native project.
How to use GitHub repository as an npm dependency in a react-native?
We make changes in open source libraries because we want the library to behave different for our project. Now consider a scenario
You went to node_modules/open_source_library
You made the changes
You are done here but in the future, there will be a case where we need to delete node_modules in order to resolve Android Studio’s linking with react-native libraries.
Now you will run npm install in order to install the open-source library. All your changes now has override by the original library. This means you need to make those changes again.
That’s why we fork the library and make changes so that we can use it as an npm dependency. Only question here is "How can we so that?”
It's very simple, You just need to run following command.
- You should be in your project directory
- npm install --save git+https://git@your fork repository URL without https://.git
You will notice that inside package.json in the dependency section, Instead of the open-source library version you will see your fork library.
That’s it. I hope this blog will help you in any way possible.
If you want to read more Do visit my website. Cheers.
Posted on February 27, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
February 27, 2020