What to do if the package is turned out broken or not fully organized?

What to do if the package is turned out broken or not fully organized?

Sometimes you find out that the package you added to your project is broken or needed some more action. As we know, after changing any code in node modules, the origin code will override your current changes after reinstalling packages. Therefore we need a permanent solution. So we have two options to fix the package.
 

  • Fork package
  • Use patch-package
     

Fork package

For forking package, firstly we need to find the repository of the package, we can find the package at https://www.npmjs.com after on the right side we can see the link of the repo:

 

After we should click Fork action and create a new Fork

 

 

Finally, we have forked the repo and now we can clone it and do the changes we wanted

 

 

We can create a new branch and fix something or add new functionality that we wanted after we should merge the brancհ. Here comes one important part, building the application in the end. We should get out how we can build the app and build it like command npm run build or something like that. And also check the .gitignore file for not blocking the build folder(remove dist or lib from gitignore file), it should be committed and pushed to the repository, otherwise after installing our repo, it will only add a few files(readme.md, package.json) to our node modules folder, not the main files that we needed. It’s ready now we just need to install our repo like package

Opening our project on Terminal, we should install our repo like
npm install https://github.com/user_name/node_project_name

 

 

It will take a little longer than usual. That’s it now we can use the changed package. And also if we check our package.json file we will see that the package install destination is from Github.

 


Patch-package

For fixing package with patch-package, first we need to install it. Type on terminal:
npm i patch-package

Using yarn we need to also install postinstall-postinstall, in terminal it should be like:
yarn add patch-package postinstall-postinstall

After installing patch-package we can go to the package which we want to change from node modules, and after doing our changes we should run command on terminal:
npx patch-package package-name

Now we can see patches folder was added to our project:
 


 

And in last we should add “postinstall”: “patch-package” script in our package.json file like:
 


 

That’s all now we can surely delete our node-modules file and run command npm install, it will also run postinstall script and add the patches that were in your project.

 

Wrap Up

So now the main question: which option to choose

If you think the changes you made will be also useful for other developers, you can use Fork and create Pull Request to upstream, so it will also help other developers. Or you are also using the package in other projects and doing fork one time you can install it in how many projects you want.

Patch-package keeps your patches colocated with the code that depends on them, and patches can be reviewed as part of your normal review process. Patch-package is done faster than a fork, fork needs some time to clone the repo, and get how to fix and build it.