Directly modifying code under node_modules is not recommended and should only be considered in emergency situations

During actual development, when we encounter A bug in node_modules, developers usually have A few options:

Method 1: Package the issue to A for others to fix and publish: be prepared for A long repair cycle or no reply.

Method 2: Pack A Mr Repair and wait for release: great, but you better hope the author sends A positive version, and the new version is backward compatible.

Method three: A package of source code drag out their own maintenance: A bit of violence and later maintenance costs are higher, but emergency can reluctantly accept.

Wait, but what if the package in question is A “ghost dependency”, such as the project’s dependency chain: A -> B -> C, then the C package has A bug. Then the changes of the above three methods need to affect packages A, B and C at the same time, so the repair cycle may be longer, but you are going to go online tonight, what can we do?

Node_modules () : node_modules () : node_modules () : node_modules ()

Patch-package can be used in the above scenarios, assuming that our current source code structure is as follows:

├ ─ ─ node_modules │ └ ─ ─ lodash │ └ ─ ─ the toString. Js ├ ─ ─ the SRC │ └ ─ ─ app. Js// Rely on loDash's toString method└ ─ ─ package. JsonCopy the code

node_modules/lodash/toString.js

var baseToString = require('./_baseToString')

function toString(value) {
  return value == null ? ' ' : baseToString(value);
}

module.exports = toString;
Copy the code

src/app.js

const toString = require('lodash/toString')
console.log(toString(123)); 
Copy the code

Suppose now need to modify the node_modules/lodash toString. Js file, you just need to follow the following steps “elegant” complete modification:

Step 1: Install dependencies

yarn add patch-package postinstall-postinstall -D
Copy the code

Step 2: Modifynode_modules/lodash/toString.jsfile

function toString(value) {
	console.log('it works!!! '); // Insert a line here
  return value == null ? ' ' : baseToString(value);
}

module.exports = toString;
Copy the code

Step 3: Generate the modification file

npx patch-package lodash
Copy the code

Patches/Lodash +4.17.21.patch are generated, and the directory structure is as follows:

├── │ ├─ │ ├─ │ ├─ │ ├─ │ ├─ │ ├─4.1721..├ ─ SRC │ ├─ ├─ package.txtCopy the code

The. Patch file is as follows:

diff --git a/node_modules/lodash/toString.js b/node_modules/lodash/toString.js
index daaf681.. 8308e76 100644
--- a/node_modules/lodash/toString.js
+++ b/node_modules/lodash/toString.js
@ @ - 22, 6 + 22, 7 @ @var baseToString = require('./_baseToString'); */ / => '1,2 '*/ function toString(value) {+  console.log('it works!!!');
   return value == null ? '' : baseToString(value);
 }
Copy the code

Step 4: Modifypackage.jsonfile

"scripts": {+"postinstall": "patch-package"
}
Copy the code

Finally, reinstall the dependency to test the final effect:

rm -rf node_modules yarn node ./src/app.js // it works!!! / / 123Copy the code

As you can see, our changes to node_modules are restored by patch-package and finally take effect, even if the dependencies are reinstalled.

At this point we’ve done a temporary patch, but it’s not really an elegant long-term solution, and in the long run we need to completely fix the third-party package defects and gradually remove the.patch files from the project.

References:

  • Github.com/ds300/patch…

Follow the wechat public account “dependency Injection” for a better reading experience.