The library is so torn, how did I patch it

Tram Ho

Surely for js programmers, everyone has used libraries on npm already

But when the library has an error, we will have to report the error and wait for the author to update the library

However, what should be done for old libraries that have not been updated for several months or even years?

The usual way is that we will have to fork our git library, and clone to fix it

Then we will fix the version of the dependency in package.json with the path of the repository that we modified with:

So the problem has been solved, the above method is also an effective solution

But in this article I will introduce another method called patch-package

Problem

The library I’m using is react-smooth-dnd for drag and drop

Everything is no problem but from react 18 version onwards, when using this library with typescript, we will get error

The first options that come to mind are to reduce the React version to 17 or change the library to declare the PropsWithChildren type for the component .

Create a patch using patch-package

The stuff in node_modules is something no one wants to touch

But this time we find the broken parts of the library in the node-modules folder and fix it

After editing, we use the command

package name is the name of the library we need to patch, here react-smooth-dnd

Done, it created us a patch folder patches the patch inside

The content inside the patch is the changes we have edited, it will help us keep the code we edited even if we delete node_modules

Finally, just running the project again solved the problem of the library’s error

And don’t forget to run the command

To create an issue to help those who encounter it know how to solve it and let the author fix this error soon

If you are interested, make a pull request too

image.png

 

Share the news now

Source : Viblo