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!
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
, 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: 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!