Home >Web Front-end >JS Tutorial >How to elegantly modify dependent libraries in node_modules? Method introduction

How to elegantly modify dependent libraries in node_modules? Method introduction

青灯夜游
青灯夜游forward
2022-01-31 09:00:312922browse

nodeHow to elegantly modify the dependent libraries of node_modules in the project? The following article will show you how to elegantly modify the dependent libraries in node_modules. I hope it will be helpful to you!

How to elegantly modify dependent libraries in node_modules? Method introduction

I recently used an open source project within the company. Due to business needs (not suitable for MR business modifications), simple modifications are needed @tencent/wuji-server-lite The source code and package.json of tnpm, and then directly reference this new package

    Method 2: Move the code copy out of node_modules as a local dependency
  • No matter which of the above This method is very awkward for people who are obsessed with code. I just changed one or two lines of code in a certain file, but I have to copy the entire project in such a bloated way. What's worse is that I may forget to modify it myself in the end. There is no diff to trace back.

  • I consulted several nodejs-ers at that time. In the past two years when I didn’t use
nodejs

, had nodejs provided any advanced methods? To solve this problem, for example, can you specify that a certain file in node_modules be replaced by another local one? The answer is no. Suddenly I had the idea to make an npm package to solve this kind of problem myself, and then I Googled it in advance as usual, but found that such a thing already existed, called patch-package. (https://www.npmjs.com/package/patch-package). It seems that the release time was two years ago. If I had not made the mobile chicken game halfway, maybe this library might have been created by me first. ,Ha ha.

Since I don’t need to reinvent a wheel, let me share how to use it. 1. Local installation, npm i patch-package

2. Add { "postinstall": "patch-package" } to the scripts of package.json, which is a hook of npm , will be executed after the dependent package is installed

3. Modify the code in node_modules

4. Generate patches, npx patch-package your-package-name

5. After submitting the patches, reinstall the package and you will find that it is the result of your modification

Finally, the official website of this library also has a very detailed comparison describing when When to fork with patch, the description is very good:

How to elegantly modify dependent libraries in node_modules? Method introduction

For more node-related knowledge, please visit:

nodejs tutorial

!

The above is the detailed content of How to elegantly modify dependent libraries in node_modules? Method introduction. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:zhihu.com. If there is any infringement, please contact admin@php.cn delete