简体   繁体   English

如何持久化在 node_modules 中所做的更改?

[英]How to persist changes made in node_modules?

I'm using Card from react-native-elements and I got rid of the我正在使用来自react-native-elements Card并且我摆脱了

{image && ..... <BackgroundImage .....

part because I wanted to add a placeholder image (activity indicator) before the image was downloaded, so that I dont just see a blank area before the image fades in.部分是因为我想在下载图像之前添加一个占位符图像(活动指示器),这样我就不会在图像淡入之前看到一个空白区域。

Anyway, I replaced that part with another component and it just hit me -- my .gitignore ignores /node_modules .无论如何,我用另一个组件替换了那个部分,它只是击中了我——我的.gitignore忽略了/node_modules If I were to make such changes as I did to the Card and then buy a new computer and clone my repo, running npm install will not give me the npm modules with my changes.如果我像对Card所做的那样进行更改,然后购买一台新计算机并克隆我的存储库,则运行npm install不会为我提供包含更改的 npm 模块。

How can I get around this?我怎样才能解决这个问题? I don't want to push my whole node_modules directory.我不想推送我的整个 node_modules 目录。 Also, I thought about just making my own component based on Card , but it has way too many dependencies and imports that I don't want to get into resolving and copying over to my project as well if I don't have to.另外,我想过只基于Card制作我自己的组件,但是它有太多的依赖项和导入,如果我不需要的话,我不想解决并复制到我的项目中。

You should never make changes to node_modules due to the reason you have described, Your changes are just temporary, and if you decide to update the dependency your changes will be lost.由于您描述的原因,您永远不应该对 node_modules 进行更改,您的更改只是暂时的,如果您决定更新依赖项,您的更改将丢失。

Depending on your reason for changing the code, you can do other things:根据您更改代码的原因,您可以执行其他操作:

  • If this is a feature, you can request it to make them, or open a pull request for them如果这是一个功能,您可以请求它来制作它们,或者为它们打开一个拉取请求
    • If its a bug, open a bug for them, or again make a pull request it to solve it如果它是一个错误,为他们打开一个错误,或者再次提出请求来解决它

If you want to make changes that persist then you have to make your own library that is based on theirs (Assuming their license allowing you yo do so), This is a really bad option, you lose all the automatic updates, and bug fixes.如果您想进行持久的更改,那么您必须基于他们的库创建自己的库(假设他们的许可证允许您这样做),这是一个非常糟糕的选择,您将丢失所有自动更新和错误修复。

What you should do is what you suggested, is that you should build a wrapper component over their component, and in that case you are not adding any new dependencies (the dependencies they use will be there anyway, unless you decide to build the component from the scratch using their dependencies, in this case this is not a Wrapper component but a whole new component, avoid this option if you can just wrap their component to achieve what you want)你应该做的是你建议的,是你应该在他们的组件上构建一个包装组件,在这种情况下你不会添加任何新的依赖项(他们使用的依赖项无论如何都会在那里,除非你决定从从头开始使用它们的依赖项,在这种情况下,这不是一个 Wrapper 组件而是一个全新的组件,如果您可以包装他们的组件来实现您想要的,请避免使用此选项)

We have solve this issue by creating update_note_modules.sh(shell executables) file and run this file after every npm/yarn install我们通过创建 update_note_modules.sh(shell 可执行文件)文件并在每次 npm/yarn 安装后运行此文件来解决此问题

Step 1: Save copy of your code from node_modules to your main application folder .第 1 步:将您的代码副本从 node_modules 保存到您的主应用程序文件夹。 We have created "core" folder and save copy there我们已经创建了“核心”文件夹并在那里保存副本

在此处输入图片说明

Step 2 : Create update_note_modules.sh file第 2 步:创建 update_note_modules.sh 文件

在此处输入图片说明

File Code档案编码

echo "Updating react-native-dropdownalert"
cp core/react-native-dropdownalert/DropdownAlert.js node_modules/react-native-dropdownalert/DropdownAlert.js

echo "node_modules updated"

Note: Path will change according to your project注意:路径将根据您的项目而变化

Step 3: Run update_note_modules.sh after every npm/yarn install第 3 步:在每次安装 npm/yarn 后运行 update_note_modules.sh

Just paste the path of file in terminal and hit enter to execute file只需在终端中粘贴文件路径并按回车键执行文件

/path to file/update_note_modules.sh

I would recommend https://github.com/ds300/patch-package here;我会在这里推荐https://github.com/ds300/patch-package makes patches and apply them consistently to things in node_modules.制作补丁并将它们一致地应用于 node_modules 中的内容。 Often useful for fixes that were never pushed to certain versions of packages.通常对于从未推送到某些版本的软件包的修复程序很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM