用自己的实现替换 npm 包 [英] Substitute an npm package with own implementation
问题描述
在我的 package.json 中,我有一个依赖项 "protractor": "2.1.0"
.该包又依赖于 "jasminewd2": "0.0.5"
.
In my package.json I have a dependency "protractor": "2.1.0"
. That package in turn depends on "jasminewd2": "0.0.5"
.
那个 jasminewd2
包有一些我想修改的行为.我下载了它的源代码并进行了我需要的更改.Yarn 的文档 讲述了使用本地源包的可能性:
That jasminewd2
package has some behavior which I want to modify. I downloaded its sources and made the changes I need. Yarn's documentation tells about a possibility to use a local source for packages:
yarn add file:/path/to/local/folder
安装本地文件系统上的包.这对于测试您尚未发布到注册表的其他包很有用.
yarn add file:/path/to/local/folder
installs a package that is on your local file system. This is useful to test out other packages of yours that haven’t been published to the registry.
当我执行那个命令时
"jasminewd2": "file:\\CustomNodeModules\\jasminewd2"
被添加到我的 package.json.这到我的 yarn.lock 文件:
"jasminewd2": "file:\\CustomNodeModules\\jasminewd2"
gets added to my package.json.And this to my yarn.lock file:
"file:\\CustomNodeModules\\jasminewd2", jasminewd2@0.0.5:
name jasminewd2
version "0.0.5"
resolved "https://registry.yarnpkg.com/jasminewd2
/-/jasminewd2-0.0.5.tgz#528609a124dfc688c1e3f434a638e047066cd63e"
因此,node_modules/jasminewd2
包含来自 npm 存储库的原始版本.如何让 yarn 安装我的版本?
As a result, node_modules/jasminewd2
contains the original version from the npm repository. How can I make yarn install my version instead?
推荐答案
我相信你的解决方案不起作用,因为 jasminewd2
是一个传递依赖(protractor
)和不是直接的.所以当你直接添加它时,传递的不受影响.
I believe your solution doesn't work because jasminewd2
is a transitive dependency (of protractor
) and not a direct one. So when you add it directly, the transitive one isn't affected.
您可以使用两种方法解决此问题:
You can work around that using two approaches:
- 如果您的更改是临时的(用于开发或故障排除),您应该按照 文档.
- 否则,您可以分叉
protractor
和jasminewd2
包并在各自的package.json
中引用它们.package.json
s 语法是量角器":
./ "
- In case your change is temporary (meant for development or troubleshooting), you should
yarn link
as described in the documentation. - Otherwise, you can fork both
protractor
andjasminewd2
packages and reference them in the respectivepackage.json
s.package.json
s syntax for that is"protractor": "<githubUser>/<githubRepo>"
.
根据我的经验,第二种方法有一个 npm 缓存形式的警告:您的 git 存储库的 HEAD
仅在 首次安装该依赖项 时被拉取.之后,它会一直缓存并每次都重新安装 - 即使您的存储库的 HEAD
已更改.
From my experience, the second approach has a caveat in the form of npm's cache: your git repo's HEAD
is only pulled when that dependency is first installed. After that, it is kept cached and keeps getting reinstalled every time - even when your repo's HEAD
has changed.
这就是为什么我通常将提交哈希作为依赖项的一部分引用,如下所示:"dependency": "user/repo.git#aef38fb2adc73304ae1ea87b0d607ad7fadc4d0g"
.我没有用 yarn
尝试这个技巧,但假设它的行为方式完全相同(通过锁文件设计).
That's why I usually reference a commit hash as part of the dependency like so: "dependency": "user/repo.git#aef38fb2adc73304ae1ea87b0d607ad7fadc4d0g"
. I didn't try this trick with yarn
, but assume it behaves the exact same way (by design via the lockfile).
这篇关于用自己的实现替换 npm 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!