不直接修改node_modules中包的解决办法

第三种:patch-package

这是一个专门用来修改node_modules中包的代码的工具,使用起来也很简单。

1、安装patch-package

npm i patch-package

2、修改node_modules

比如我想修改包A,那么我直接进node_modules中修改,然后执行

npx patch-package A

这个时候你的项目根目录下会出现patches这个目录,里面会出现一个包A的补丁文件,这个文件可大有用处!!

注意:记得要把patches这个目录提交到git或者svn

其实这个时候你本地已经使用到了你修改后的代码了,但问题是怎么让你的同事也同步到你的修改后的代码。。

3、"postinstall": "patch-package"

package.jsonscript中增加

"postinstall": "patch-package"

这个命令的作用就是:当你的同事去执行npm i的时候,会自动执行npm run postinstall这个命令,也就是执行patch-package,这时候就回去读取刚刚的patches目录,并将那些补丁达到对应的包里。

比如我刚刚修改了包A并提交了补丁文件。那么我同事npm i的时候,就会自动执行npm run postinstall这个命令,然后去pacthes目录中找到对应的包A的补丁文件,并将补丁打到node_modules的包A中,达到同步修改代码的效果!!!

C:\Windows\System32>cnpm -v 'cnpm' 是内部或外部命令,也是可运行的程序 或批处理文件。 C:\Windows\System32>npm config set registry https://registry.npmmirror.com/ C:\Windows\System32>npm install -g cnpm --registry=https://registry.npmmirror.com npm ERR! code EPERM npm ERR! syscall rename npm ERR! path C:\Program File\nodejs\node_global\node_modules\cnpm\node_modules\.make-fetch-happen.DELETE\node_modules\@npmcli npm ERR! dest C:\Program File\nodejs\node_global\node_modules\cnpm\node_modules\make-fetch-happen\node_modules\@npmcli npm ERR! errno -4048 npm ERR! Error: EPERM: operation not permitted, rename 'C:\Program File\nodejs\node_global\node_modules\cnpm\node_modules\.make-fetch-happen.DELETE\node_modules\@npmcli' -> 'C:\Program File\nodejs\node_global\node_modules\cnpm\node_modules\make-fetch-happen\node_modules\@npmcli' npm ERR! [OperationalError: EPERM: operation not permitted, rename 'C:\Program File\nodejs\node_global\node_modules\cnpm\node_modules\.make-fetch-happen.DELETE\node_modules\@npmcli' -> 'C:\Program File\nodejs\node_global\node_modules\cnpm\node_modules\make-fetch-happen\node_modules\@npmcli'] { npm ERR! cause: [Error: EPERM: operation not permitted, rename 'C:\Program File\nodejs\node_global\node_modules\cnpm\node_modules\.make-fetch-happen.DELETE\node_modules\@npmcli' -> 'C:\Program File\nodejs\node_global\node_modules\cnpm\node_modules\make-fetch-happen\node_modules\@npmcli'] { npm ERR! errno: -4048, npm ERR! code: 'EPERM', npm ERR! syscall: 'rename', npm ERR! path: 'C:\\Program File\\nodejs\\node_global\\node_modules\\cnpm\\node_modules\\.make-fetch-happen.DELETE\\node_modules\\@npmcli', npm ERR! dest: 'C:\\Program File\\nodejs\\node_global\\node_modules\\cnpm\\node_modules\\make-fetch-happen\\node_modules\\@npmcli' npm ERR! }, npm ERR! errno: -4048, npm ERR! code: 'EPERM', npm ERR! syscall: 'rename', npm ERR! path: 'C:\\Program File\\nodejs\\node_global\\node_modules\\cnpm\\node_modules\\.make-fetch-happen.DELETE\\node_modules\\@npmcli', npm ERR! dest: 'C:\\Program File\\nodejs\\node_global\\node_modules\\cnpm\\node_modules\\make-fetch-happen\\node_modules\\@npmcli', npm ERR! parent: 'cnpm' npm ERR! } npm ERR! npm ERR! The operation was rejected by your operating system. npm ERR! It's possible that the file was already in use (by a text editor or antivirus), npm ERR! or that you lack permissions to access it. npm ERR! npm ERR! If you believe this might be a permissions issue, please double-check the npm ERR! permissions of the file and its containing directories, or try running npm ERR! the command again as root/Administrator. npm ERR! A complete log of this run can be found in: npm ERR! C:\Program File\nodejs\node_cache\_logs\2025-06-10T12_16_03_496Z-debug.log
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值