Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you modify other's library/ module?

I want to add a Card component to this module: https://snack.expo.io/@xcarpentier/gifted-chat (demo)

For example, if using onLongPress() on the Bubble Message, i want additional info to appear (right below that Bubble Message, as a small card, like Tinder Card).

How do I do that? Do I need to clone the source code and then modify it to fit what I need?

like image 747
Vũ Nguyễn Duy Avatar asked Jun 02 '18 22:06

Vũ Nguyễn Duy


1 Answers

Although you are able to edit the file in your node_modules folder, it is not a great long-term solution. Why not?

  • The process is not consistent with using other modules
  • Another npm install will overwrite your changes
  • Your solution won't be available to someone else wanting to implement that feature

Bad Solution

If you would still like to go this route, the quickest way to go about it would be by linking it via npm. In the event this link is not available anymore, you can link a module following these steps:

  1. In your terminal, navigate to the node module you have modified
  2. Create a global symlink with npm link
  3. Navigate to your app's root directory
  4. Reference that symlink with npm link name-of-module

Again, this is not a permanent solution and should only be used for quickly testing modifications to a module.

Better Solution

Forking the repo is a good way to maintain the commits specific to that module, and you can share your modifications to the open-source community. Some reasons to fork are explained in the Github help wiki, but doing it is pretty straight-forward.

  1. Navigate to the Github repo of the package you are wanting to change
  2. Press the Fork button in the top right corner
  3. npm install git+your-forked-repo-url in your project's root directory (don't forget to npm uninstall the old one)

Now, you can follow the process mentioned in the Bad Solution to locally test out changes to that package. After you're satisfied with them, you can copy those changes over to your forked repo and push them to Github (you'll want bump your version, but you may have some merge conflicts to resolve if you ever want to merge changes with the upstream repo). Then do another npm install of your repo make those changes more permanent in your node_modules folder.

If you would like to stay up-to-date with the repo you forked from, Github explains the process here.

TL;DR

Choose the Better Solution.

like image 189
Ezra Avatar answered Sep 28 '22 06:09

Ezra