修补 npm 包时出现错误

Getting errors when patching a npm package

当我像这样修补依赖项时:

package.json:

  "dependencies": 
    "react-native-webview": "patch:react-native-webview@11.0.3#scripts/patches/react-native-webview+11.0.3.patch",
    ...

patches/react-native-webview+11.0.3.patch:

diff --git a/apple/RNCWebView.m b/apple/RNCWebView.m
index ff9ff9e0..dfea91a8 100644
--- a/apple/RNCWebView.m
+++ b/apple/RNCWebView.m
@@ -194,12 +194,18 @@ - (void)dealloc
...

当我使用 yarnyarn install 在本地安装时一切正常。补丁已应用,没有错误。

但是,当事情进入 CI 时,我收到如下错误:

**ERROR** Failed to apply patch for package react-native-webview at path
  
    node_modules/react-native-webview

  This error was caused because patch-package cannot apply the following patch file:

    patches/react-native-webview+11.0.3.patch

  Try removing node_modules and trying again. If that doesn't work, maybe there was
  an accidental change made to the patch file? Try recreating it by manually
  editing the appropriate files and running:
  
    patch-package react-native-webview
  
  If that doesn't work, then it's a bug in patch-package, so please submit a bug
  report. Thanks!

    https://github.com/ds300/patch-package/issues

如果我将补丁更改为使用像 a/node_modules/react-native-webview/apple/RNCWebView.m 这样的路径,情况也不会好转。然后我得到一个错误

➤ YN0001: │ Error: react-native-webview@patch:react-native-webview@npm%3A11.0.3#../../scripts/patches/react-native-webview+11.0.3.patch::version=11.0.3&hash=d13297&locator=proj-core%40workspace%3Alib%2Fproj-core: ENOENT: no such file or directory, lstat '/node_modules/react-native-webview/node_modules/react-native-webview/apple/RNCWebView.m'
    at rn (/home/user/dev/proj-client/.yarn/releases/yarn-3.0.0.cjs:172:9625)
    at kn (/home/user/dev/proj-client/.yarn/releases/yarn-3.0.0.cjs:172:9889)
    at Qr.resolveFilename (/home/user/dev/proj-client/.yarn/releases/yarn-3.0.0.cjs:177:21594)
    at Qr.resolveFilename (/home/user/dev/proj-client/.yarn/releases/yarn-3.0.0.cjs:177:21498)
    at Qr.resolveFilename (/home/user/dev/proj-client/.yarn/releases/yarn-3.0.0.cjs:177:21498)
    at Qr.lstatSync (/home/user/dev/proj-client/.yarn/releases/yarn-3.0.0.cjs:177:18712)
    at Qr.lstatPromise (/home/user/dev/proj-client/.yarn/releases/yarn-3.0.0.cjs:177:18671)
    at yt.lstatPromise (/home/user/dev/proj-client/.yarn/releases/yarn-3.0.0.cjs:177:34701)
    at Zu (/home/user/dev/proj-client/.yarn/releases/yarn-3.0.0.cjs:578:7908)
    at YB (/home/user/dev/proj-client/.yarn/releases/yarn-3.0.0.cjs:580:184)

这里的问题是您使用两种不同的工具来修补 npm 包,并且在本地使用与 CI 不同的工具:

  • patch-package 作为 post 安装脚本(如果您检查 package.json 中的“postinstall”条目,您会看到类似 patch-package --patch-dir ./scripts/patches)
  • yarn 内置的 package patching 东西,在 package.json.
  • 中使用了 patch:

但是,它们的工作方式不同:

patch-package yarn's patch:
applies the patch from the project root, so you need to include node_modules/package-name in the diff. applies the patch from the package install directory, so you don't need to do that
requires that the patch be named package-name+version.patch doesn't require any specific naming

这里的解决方案是选择一个,然后更改补丁以与该系统一起使用。因此,例如,如果您重命名文件以删除 +version,您将能够仅使用 yarn 的 patch:.

继续前进

不知道现在还有没有人关注这个问题

我的问题是 已解决 版本在 yarn.lock 中的差异。

我有一个不同的 package.json 版本,我复制了代码但它不起作用,因为 yarn.lock 已经生成了。 我刚刚复制了旧的 yarn 文件,它工作得很好。

例如,我的 yarn.lock 看起来像这样

我更改了它以匹配正确的版本