如何将项目里node_modules的更改保存起来

本文介绍如何使用patch-package包在前端项目中保存并应用对node_modules包的自定义修改,以便于团队协作。通过postinstall脚本和git版本控制实现依赖问题的一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在前端项目开发中,我们经常会遇到node_modules里的包有问题,当自己手动更改成正确的后,自己没问题了,但是别人重新npm install时还会有同样的问题,那么如何将node_modules的更改保存到项目中呢,这样别人在生成依赖后能有一个正常的环境

通过:

patch-package
https://www.npmjs.com/package/patch-package

这个包可以在npm install后按照项目里保存的更改自动修改node_modules,非常好用

使用步骤:

一.安装

可以使用npm

npm i patch-package -D

yarn(postinstall-postinstall 确保在yarn remove时依旧能保持更正代码)

yarn add patch-package postinstall-postinstall

二.设置

安装完后需要给package.json设置postinstall,在安装完node_modules后会自动执行

"scripts": {
  "postinstall": "patch-package"
}

三.使用

修改node_modules的内容
npx patch-package 修改的包名(执行完成后,会在项目目录生成一个文件夹,保存着此次修改的内容)
通过git推到远程仓库,这样其他小伙伴也可以用了

PS D:\前端\苍穹外卖\前端源码\苍穹外卖前端源码\project-sky-admin-vue-ts> npm install -g cnpm --registry=https://registry.npmmirror.com --force npm WARN using --force I sure hope you know what you are doing. npm ERR! code EPERM npm ERR! syscall rename npm ERR! path D:\My Software\nvm\nvm\v14.20.1\node_modules\cnpm\node_modules\.make-fetch-happen.DELETE\node_modules\@npmcli npm ERR! dest D:\My Software\nvm\nvm\v14.20.1\node_modules\cnpm\node_modules\make-fetch-happen\node_modules\@npmcli npm ERR! errno -4048 npm ERR! Error: EPERM: operation not permitted, rename 'D:\My Software\nvm\nvm\v14.20.1\node_modules\cnpm\node_modules\.make-fetch-happen.DELETE\node_modules\@npmcli' -> 'D:\My Software\nvm\nvm\v14.20.1\node_modules\cnpm\node_modules\make-fetch-happen\node_modules\@npmcli' npm ERR! [OperationalError: EPERM: operation not permitted, rename 'D:\My Software\nvm\nvm\v14.20.1\node_modules\cnpm\node_modules\.make-fetch-happen.DELETE\node_modules\@npmcli' -> 'D:\My Software\nvm\nvm\v14.20.1\node_modules\cnpm\node_modules\make-fetch-happen\node_modules\@npmcli'] { npm ERR! cause: [Error: EPERM: operation not permitted, rename 'D:\My Software\nvm\nvm\v14.20.1\node_modules\cnpm\node_modules\.make-fetch-happen.DELETE\node_modules\@npmcli' -> 'D:\My Software\nvm\nvm\v14.20.1\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: 'D:\\My Software\\nvm\\nvm\\v14.20.1\\node_modules\\cnpm\\node_modules\\.make-fetch-happen.DELETE\\node_modules\\@npmcli', npm ERR! dest: 'D:\\My Software\\nvm\\nvm\\v14.20.1\\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: 'D:\\My Software\\nvm\\nvm\\v14.20.1\\node_modules\\cnpm\\node_modules\\.make-fetch-happen.DELETE\\node_modules\\@npmcli', npm ERR! dest: 'D:\\My Software\\nvm\\nvm\\v14.20.1\\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:\Users\liuchaokun\AppData\Roaming\npm-cache\_logs\2025-07-29T11_03_42_433Z-debug.log
最新发布
07-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值