Home >Web Front-end >JS Tutorial >A brief discussion on how to manually configure dependency packages in node_modules

A brief discussion on how to manually configure dependency packages in node_modules

青灯夜游
青灯夜游forward
2021-10-27 09:58:394162browse

How to manually configure dependency packages in node_modules? The following article will introduce to you how to use patch-package to modify the dependent packages in node_modules. I hope it will be helpful to you!

A brief discussion on how to manually configure dependency packages in node_modules

First of all, the problem happened like this. I used the pdfvuer third-party plug-in in the project to display the successfully stamped PDF. At this time, I found that the article could be properly It was displayed, but the company's signature could not be displayed. At this time, I finally found a solution! ! However, the modified code that displays the signature is in the path of the dependent package node_modules. After all, files modified under node_modules need to be modified manually every time npm install is performed. Instantly I started to feel uncomfortable. I felt uncomfortable, but I still had to find a way to solve it

A brief discussion on how to manually configure dependency packages in node_modules

At first I thought of two solutions:

  • Fork other people's code to your own warehouse, and after modification, install this plug-in from your own warehouse.

  • Download other people's code locally, put it in the src directory, and introduce it manually after modification. There are also many inconveniences in doing this. For example, packaging components individually is cumbersome and time-consuming, and it also makes the project look bloated.

However, any of the above methods are too complicated. I just changed one or two lines of code in a certain file, but I had to copy the entire project in such a bloated way. In the end, I might not be able to do it myself. I forgot where I modified it, and the update is troublesome. We need to manually update the code every time, and cannot update it synchronously with the plug-in. In this case, I turned around and started my quest for knowledge again. . . Then I found a superior solution to use patch-package to modify the dependent packages in node_modules. [Recommended learning: "nodejs Tutorial"]

Solution

1. Install patch-package# through the command ##

`npm install patch-package --save-dev`

2. Modify the package.json file in the project root directory

在 package.json 文件中的 scripts 中加入 "postinstall": "patch-package"

A brief discussion on how to manually configure dependency packages in node_modules

##3. Manually modify the node_modules dependency package The source code in

A brief discussion on how to manually configure dependency packages in node_modules##4. Manually execute the command to create the npx patch-package package-name patch file

`npx patch-package package-name`
where package-name refers to the name of the dependent package to be modified. After executing this command, a patches folder will be automatically created in the project root directory, and a patch file named package-name version.patch will appear in the folder. As shown in the picture:

For example: I want to modify the file under pdfjs-dist, so the command I executed is: npx patch-package pdfjs-distA brief discussion on how to manually configure dependency packages in node_modules

A brief discussion on how to manually configure dependency packages in node_modules5. Test whether the patch package is effective

Manually delete the node_modules file in the project (forced deletion of the node_modules folder: rimraf node_modules), and restart Execute the npm install command to install the node_modules dependency package. After the installation is successful, check the files in the node_modules dependency package that you modified before to see if the code you modified still exists. If the code you modified before still exists, it means that the patch file has taken effect. If the code you modified before does not exist, it means that the patch file does not exist. Take effect.

For more programming-related knowledge, please visit:

Introduction to Programming

! !

The above is the detailed content of A brief discussion on how to manually configure dependency packages in node_modules. For more information, please follow other related articles on the PHP Chinese website!

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