"**ERROR** Failed to apply patch for package react-native at path" After deleting node_modules

See original GitHub issue
 **ERROR** Failed to apply patch for package react-native at path

    node_modules/react-native

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

    patches/react-native+0.64.2.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

  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

I’m currently migrating my react native version from 0.63.4 to 0.64.2 I’m running:

  • MacOS Big Sur 11.4
  • Node 12.15.0
  • React Native 0.64.2

I added some patches for multiple files but including a patch for the generate-specs.sh file from react-native, I’m able to generate it successfully running yarn patch-package react-native but after a fresh dependencies install (like removing the node_modules) and install them again from scratch, the patch fails showing *ERROR** Failed to apply patch for package react-native at path does someone know how could I fix this issue? this is only happening for the react-native patch, I don’t have issues with the others that I have and where generated in the same way

My patch looks like this:

diff --git a/node_modules/react-native/ReactAndroid/src/main/jni/first-party/fbjni/.npmignore b/node_modules/react-native/ReactAndroid/src/main/jni/first-party/fbjni/.gitignore
similarity index 100%
rename from node_modules/react-native/ReactAndroid/src/main/jni/first-party/fbjni/.npmignore
rename to node_modules/react-native/ReactAndroid/src/main/jni/first-party/fbjni/.gitignore
diff --git a/node_modules/react-native/ReactCommon/hermes/inspector/tools/msggen/.npmignore b/node_modules/react-native/ReactCommon/hermes/inspector/tools/msggen/.gitignore
similarity index 100%
rename from node_modules/react-native/ReactCommon/hermes/inspector/tools/msggen/.npmignore
rename to node_modules/react-native/ReactCommon/hermes/inspector/tools/msggen/.gitignore
diff --git a/node_modules/react-native/scripts/generate-specs.sh b/node_modules/react-native/scripts/generate-specs.sh
index aed955e..ddac05b 100755
--- a/node_modules/react-native/scripts/generate-specs.sh
+++ b/node_modules/react-native/scripts/generate-specs.sh
@@ -10,14 +10,14 @@
 #
 # Optionally, set these envvars to override defaults:
 # - SRCS_DIR: Path to JavaScript sources
-# - CODEGEN_MODULES_LIBRARY_NAME: Defaults to FBReactNativeSpec
-# - CODEGEN_MODULES_OUTPUT_DIR: Defaults to React/$CODEGEN_MODULES_LIBRARY_NAME/$CODEGEN_MODULES_LIBRARY_NAME
-# - CODEGEN_COMPONENTS_LIBRARY_NAME: Defaults to rncore
-# - CODEGEN_COMPONENTS_OUTPUT_DIR: Defaults to ReactCommon/react/renderer/components/$CODEGEN_COMPONENTS_LIBRARY_NAME
+# - MODULES_LIBRARY_NAME: Defaults to FBReactNativeSpec
+# - MODULES_OUTPUT_DIR: Defaults to React/$MODULES_LIBRARY_NAME/$MODULES_LIBRARY_NAME
+# - COMPONENTS_LIBRARY_NAME: Defaults to rncore
+# - COMPONENTS_OUTPUT_DIR: Defaults to ReactCommon/react/renderer/components/$COMPONENTS_LIBRARY_NAME
 #
 # Usage:
 #   ./scripts/generate-specs.sh
-#   SRCS_DIR=myapp/js CODEGEN_MODULES_LIBRARY_NAME=MySpecs CODEGEN_MODULES_OUTPUT_DIR=myapp/MySpecs ./scripts/generate-specs.sh
+#   SRCS_DIR=myapp/js MODULES_LIBRARY_NAME=MySpecs MODULES_OUTPUT_DIR=myapp/MySpecs ./scripts/generate-specs.sh
 #
 
 # shellcheck disable=SC2038
@@ -27,13 +27,13 @@ set -e
 THIS_DIR=$(cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")")" && pwd)
 TEMP_DIR=$(mktemp -d /tmp/react-native-codegen-XXXXXXXX)
 RN_DIR=$(cd "$THIS_DIR/.." && pwd)
-
-# find node path
-source "$RN_DIR/scripts/find-node.sh"
-
 NODE_BINARY="${NODE_BINARY:-$(command -v node || true)}"
 USE_FABRIC="${USE_FABRIC:-0}"
 
+# Find path to Node
+# shellcheck source=/dev/null
+source "$RN_DIR/scripts/find-node.sh"
+
 cleanup () {
   set +e
   rm -rf "$TEMP_DIR"
@@ -46,25 +46,34 @@ describe () {
 
 main() {
   SRCS_DIR=${SRCS_DIR:-$(cd "$RN_DIR/Libraries" && pwd)}
-  CODEGEN_MODULES_LIBRARY_NAME=${CODEGEN_MODULES_LIBRARY_NAME:-FBReactNativeSpec}
+  MODULES_LIBRARY_NAME=${MODULES_LIBRARY_NAME:-FBReactNativeSpec}
 
-  CODEGEN_COMPONENTS_LIBRARY_NAME=${CODEGEN_COMPONENTS_LIBRARY_NAME:-rncore}
-  CODEGEN_MODULES_OUTPUT_DIR=${CODEGEN_MODULES_OUTPUT_DIR:-"$RN_DIR/React/$CODEGEN_MODULES_LIBRARY_NAME/$CODEGEN_MODULES_LIBRARY_NAME"}
-  # TODO: $CODEGEN_COMPONENTS_PATH should be programmatically specified, and may change with use_frameworks! support.
-  CODEGEN_COMPONENTS_PATH="ReactCommon/react/renderer/components"
-  CODEGEN_COMPONENTS_OUTPUT_DIR=${CODEGEN_COMPONENTS_OUTPUT_DIR:-"$RN_DIR/$CODEGEN_COMPONENTS_PATH/$CODEGEN_COMPONENTS_LIBRARY_NAME"}
+  COMPONENTS_LIBRARY_NAME=${COMPONENTS_LIBRARY_NAME:-rncore}
+  MODULES_OUTPUT_DIR=${MODULES_OUTPUT_DIR:-"$RN_DIR/React/$MODULES_LIBRARY_NAME/$MODULES_LIBRARY_NAME"}
+  # TODO: $COMPONENTS_PATH should be programmatically specified, and may change with use_frameworks! support.
+  COMPONENTS_PATH="ReactCommon/react/renderer/components"
+  COMPONENTS_OUTPUT_DIR=${COMPONENTS_OUTPUT_DIR:-"$RN_DIR/$COMPONENTS_PATH/$COMPONENTS_LIBRARY_NAME"}
 
   TEMP_OUTPUT_DIR="$TEMP_DIR/out"
   SCHEMA_FILE="$TEMP_DIR/schema.json"
 
+  CODEGEN_REPO_PATH="$RN_DIR/packages/react-native-codegen"
+  CODEGEN_NPM_PATH="$RN_DIR/../react-native-codegen"
+
   if [ -z "$NODE_BINARY" ]; then
     echo "Error: Could not find node. Make sure it is in bash PATH or set the NODE_BINARY environment variable." 1>&2
     exit 1
   fi
 
-  CODEGEN_PATH=$("$NODE_BINARY" -e "console.log(require('path').dirname(require.resolve('react-native-codegen/package.json')))")
+  if [ -d "$CODEGEN_REPO_PATH" ]; then
+    CODEGEN_PATH=$(cd "$CODEGEN_REPO_PATH" && pwd)
+  elif [ -d "$CODEGEN_NPM_PATH" ]; then
+    CODEGEN_PATH=$(cd "$CODEGEN_NPM_PATH" && pwd)
+  else
+    echo "Error: Could not determine react-native-codegen location. Try running 'yarn install' or 'npm install' in your project root." 1>&2
+    exit 1
+  fi
 
-  # Special case for running CodeGen from source: build it
   if [ ! -d "$CODEGEN_PATH/lib" ]; then
     describe "Building react-native-codegen package"
     bash "$CODEGEN_PATH/scripts/oss/build.sh"
@@ -75,14 +84,14 @@ main() {
 
   describe "Generating native code from schema (iOS)"
   pushd "$RN_DIR" >/dev/null || exit 1
-    "$NODE_BINARY" scripts/generate-specs-cli.js ios "$SCHEMA_FILE" "$TEMP_OUTPUT_DIR" "$CODEGEN_MODULES_LIBRARY_NAME"
+    "$NODE_BINARY" scripts/generate-specs-cli.js ios "$SCHEMA_FILE" "$TEMP_OUTPUT_DIR" "$MODULES_LIBRARY_NAME"
   popd >/dev/null || exit 1
 
   describe "Copying output to final directory"
-  mkdir -p "$CODEGEN_COMPONENTS_OUTPUT_DIR" "$CODEGEN_MODULES_OUTPUT_DIR"
-  cp -R "$TEMP_OUTPUT_DIR/$CODEGEN_MODULES_LIBRARY_NAME.h" "$TEMP_OUTPUT_DIR/$CODEGEN_MODULES_LIBRARY_NAME-generated.mm" "$CODEGEN_MODULES_OUTPUT_DIR" || exit 1
-  find "$TEMP_OUTPUT_DIR" -type f | xargs sed -i.bak "s/$CODEGEN_MODULES_LIBRARY_NAME/$CODEGEN_COMPONENTS_LIBRARY_NAME/g" || exit 1
-  find "$TEMP_OUTPUT_DIR" -type f -not -iname "$CODEGEN_MODULES_LIBRARY_NAME*" -exec cp '{}' "$CODEGEN_COMPONENTS_OUTPUT_DIR/" ';' || exit 1
+  mkdir -p "$COMPONENTS_OUTPUT_DIR" "$MODULES_OUTPUT_DIR"
+  cp -R "$TEMP_OUTPUT_DIR/$MODULES_LIBRARY_NAME.h" "$TEMP_OUTPUT_DIR/$MODULES_LIBRARY_NAME-generated.mm" "$MODULES_OUTPUT_DIR" || exit 1
+  find "$TEMP_OUTPUT_DIR" -type f | xargs sed -i.bak "s/$MODULES_LIBRARY_NAME/$COMPONENTS_LIBRARY_NAME/g" || exit 1
+  find "$TEMP_OUTPUT_DIR" -type f -not -iname "$MODULES_LIBRARY_NAME*" -exec cp '{}' "$COMPONENTS_OUTPUT_DIR/" ';' || exit 1
 
   echo >&2 'Done.'
 }

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
AmaarHasssancommented, Feb 17, 2022

I don’t know if anyone still looks up for this issue. My issue was the difference of resolved version in yarn.lock. I had a different package.json versions in my case, and i copied the code and it didn’t work because the yarn.lock was already generated. I just copied over the old yarn file and it worked perfectly.

For instance image this was causing the error and i changed it to match the exact version image

0reactions
inacio0196commented, Dec 13, 2022

I make a downgrade "patch-package": "^5.0.0",

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting errors when patching a npm package - Stack Overflow
The patch gets applied, and there are no errors. However, when things go into CI, I get an error like the following: **ERROR**...
Read more >
patch-package - Bountysource
```ERROR Failed to apply patch for package electron-builder. This happened because the patch file patches/electron-builder+22.9.1.patch could not be parsed.
Read more >
Easier React Native upgrade with patch-package - Novanet blog
This utility will generate a .patch file, which you can commit to your repository. And then patch-package will re-apply the fix everytime ...
Read more >
patch-package - npm
When running locally patch-package always exits with 0 by default. This happens even after failing to apply patches because otherwise yarn.lock ...
Read more >
Troubleshooting - React Native
If you run into issue where executing npm run android on macOS throws the above error, try to run sudo chmod +x android/gradlew...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found