DEV Community

LiuClassmate
LiuClassmate

Posted on

Modify node_modules directly? You are not very elegant!

Why do we need to modify the files in the node_modules?

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

For example, a scene I encountered last time. I used a middleware package for interface forwarding, but I encountered a problem. This package limits the format of uploaded files.

In fact, I want to let go of the restrictions on file formats and support 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 do I change it? This is a problem. Or, how should I change it to be the best?

1. Modify directly

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

But in fact, this approach has too many disadvantages!

  • It can be used only in your local environment, your colleagues can't use it.
  • The code of that package will be restored to its original state after executing npm i command.

So no one should do it.

2. Maintain a package independently

I used a package named A, which restricts the format of the uploaded file, but I don't want it, I want to let go of the restrictions, so what can I do?

I can do it like this. I write a new package B, which is actually a copy of package A, and after finishing the copy, I let go of the restrictions of package B, and then publish package B to the npm platform.

I don't need to use package A in my project anymore at this time, I just need to install package B I maintain. Although this can also achieve that effect, it will increase the maintenance cost.

3. patch-package

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

3.1 Install patch-package

npm i patch-package
Enter fullscreen mode Exit fullscreen mode

3.2 Modify node_modules

For example, If I want to modify package A, then I modify it directly in node_modules, and I execute this command:
npx patch-package A

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

Remember to submit the patches directory to Git.

In fact, at this time, you have already used the code modified by yourself, but the question is how to make your colleagues synchronize with the code.

3.3 "postinstall": "patch-package"

Add command in the script of package.json file

"postinstall": "patch-package"
Enter fullscreen mode Exit fullscreen mode

The function of this command is: when your colleague executes npm i, the command npm run postinstall will be executed automatically, which means that it will read the patches directory and put those patches into the corresponding package.

Top comments (0)