在 node 模块文件夹中编辑 react npm 模块的代码 [英] Editing code of react npm modules in node module folder

查看:21
本文介绍了在 node 模块文件夹中编辑 react npm 模块的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 modules 文件夹中更改 npm 模块的代码?我认为这不是推荐的做法,还有其他方法可以实现吗?目前,我尝试更改模块目录中的代码,但更改似乎并不适用.提前致谢.

Is it possible to change code of npm modules in the modules folder? I assume this is not recommended practice, are there any other ways achieving this? Currently, I tried changing the code in the module's directory but the changes doesn't seem to apply. Thanks in advance.

推荐答案

当然你可以在 node_modules 中改变包的内容,因为它是一个标准化的格式.但是,您不应该这样做,因为您应该提交更改并重新分发它们.

Of course you can change the contents of packages in node_modules as it's a standardized format. However, you shouldn't do that because you should be committing your changes and redistributing them.

不幸的是,这个问题的解决方案并不简单,这是我过去一直在努力解决的问题.

Unfortunately, the solution to this is kind of non-trivial and it's something I've struggled with in the past.

第一种方法是在本地克隆 repo 并使用 npm link 在您的项目中使用它.

The first approach is to clone the repo locally and use npm link to use it in your project.

npm link ../path/to/my/proj

这种方法的缺点是您仍然必须手动下载存储库才能使用它,并且 npm link 使您的链接版本 包可以在您的系统上全局使用这可能会产生意想不到的副作用.话虽如此,npm link 可能是最好的方法,如果您想在本地测试对包的更改并将它们贡献到上游.

The drawback with this approach is that you still have to manually download the repository to use it and npm link makes your linked version the package to use globally on your system which may have unintended side effects. That being said, npm link is probably the best approach if you want to locally test changes to your package and contribute them upstream.

您也可以通过执行以下操作直接从分叉的 git 存储库安装它:

You could also directly install it from a forked git repo by doing:

npm install --save $GIT_REPO_URL

但是使用这种方法,您需要拥有访问 git 存储库的凭据,因此在处理私有存储库等时会出现额外的复杂性,尤其是在处理 CI 环境时.此外,您应该包含一个提交,以便您可以获得可重复的构建 - 但是,在不使用 npm 链接的情况下开发它有点痛苦.您可以查阅 npm 文档以了解其他安装选项或更详细的信息.如果您不必担心这些事情,这是一个非常好的方法.

But with this approach, you need to have the credentials to access the git repo and thus additional complexity arises when you are dealing with private repos and the such, particularly when dealing with CI environments. Also, you should include a commitish so that you can get reproducible builds -- it's sort of a pain to develop this without using npm link, though. You can consult the npm documentation for other install options or for more specifics. This is a pretty good approach if you don't have to worry about any of those things.

完成更改后,您还可以将分叉版本安装到您​​的项目中,如下所示:

Once you've made your changes, you could also install the forked version into your project like so:

npm install --save ../path/to/my/proj

但是,那么,您实际上可以让其他 NPM 项目成为您项目的一部分,其中包含 git 子模块、git 子树或使用 monorepo 之类的东西.对于团队来说,这可能是一个很好的方法,但对于您正在尝试做的事情可能有点过头了,而且您需要考虑很多工具才能使其成为一种好方法.

However, then, you'd effectively have the other NPM project a part of your project with something like git submodules, git subtrees, or using a monorepo. This may be a good approach for a team, but is probably overkill for what you're trying to do and there's seriously a lot of tooling that you need to think about to make this a good approach.

不幸的是,所有先前的方法都假设包没有构建过程,或者它们是使用 npm 的安装后脚本.但是,一些 npm 模块是通过发布特定的构建目录来编写的,这使得 npm 上的内容与源代码有很大不同.

Unfortunately, all the prior approaches kind of assume that the packages either do not have a build process or that they get built automatically with something like npm's postinstall scripts. However, some npm modules are written by publishing a specific build directory, making what's on npm significantly different from the source.

在这些情况下,您需要将构建发布到 npm 可以安装的位置.这包括公共 范围包、私有 npm 存储库或发布您的npm 模块到个人工件服务器.

In these cases, you need to publish your builds to somewhere that npm can install from. This includes things like a public scoped package, a private npm repositories, or publishing your npm module to a personal artifact server.

这篇关于在 node 模块文件夹中编辑 react npm 模块的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆