How to modify node_modules

 Why modify node_modules?

In normal development, it is rarely necessary to change the code in node_modules, but there are many cases that need to be modified.

For example, I encountered a scene last time, I used a middleware package for interface forwarding, but I encountered a problem: this package limits the uploaded file format

But in fact, I want to let go of uploading all file formats, so I am forced to modify the code of this package in node_modules and let go of its restrictions, so as to achieve the effect I want, so how should I change it? This is a problem


The first method: direct change

This is easy to understand, just go directly to node_modules, find the code of that package, modify the code in the corresponding place, and then restart the project to achieve the effect I want.

But in fact, this approach has too many disadvantages!


1. You can only use the code you modified locally, and your colleagues cannot use it

2. After the next npm i, the code of the package will be restored to its original state

So no one should do it? ? ?


The second: maintain a package independently

I use a package A, which restricts the format of the uploaded file, but I don't want to, I want to let go of the restriction, so how can I do it?


I can do this, I write a new package B, this new package B is actually a copy of package A, and after copying, I release the format restrictions in package B, and then push package B to npm

At this time, I don't need to use package A in my project, I just need to install package B that I maintain myself. This can actually achieve the effect, but it will increase the cost of maintenance. . .


The third type: patch-package

This is a tool specially used to modify the code of the package in node_modules, and it is very simple to use.

1. npm i patch-package


2.Modify node_modules

For example, if I want to modify package A, then I directly modify it in node_modules, and then execute

npx patch-package A

At this time, the patches directory will appear in the root directory of your project, and a patch file for package A will appear in it. 


Note: Remember to submit the patches directory to git or svn

In fact, at this time, you have already used your modified code locally, but the question is how to let your colleagues also synchronize to your modified code


3、"postinstall": "patch-package"

Add in the script of package.json

"postinstall":"patch-package"

The function of this command is: when your colleague executes npm i, it will automatically execute the npm run postinstall command, that is, execute patch-package. At this time, go back and read the patches directory just now, and match those patches to the corresponding in the bag.


For example, I just modified package A and submitted the patch file. Then when my colleague npm i, it will automatically execute the npm run postinstall command, then go to the pacthes directory to find the patch file of the corresponding package A, and put the patch into the package A of node_modules to achieve the effect of modifying the code synchronously! !

Popular posts from this blog

大学资料分享——广工计院各个科目实验报告、课设及期末复习资料!!

JAVA Traffic Signal Light Course Design (Source Code + Report + Video Demonstration)

Win10 远程计算机或设备将不接受连接