12

I'm continuously getting Require cycles are allowed, but can result in uninitialized values. Consider refactoring to remove the need for a cycle. warning on my terminal and it has make debugging my program so difficult.

I have used

YellowBox.ignoreWarnings([
  'Require cycle:', 
])

and also

console.disableYellowBox = true;

But neither worked. YellowBox.ignoreWarnings worked only for the warnings displayed on the mobile but not for the warnings on the terminal of my IDE. How can i prevent getting these warnings on the terminal.

CraZyDroiD
  • 6,622
  • 30
  • 95
  • 182
  • 1
    there is no way to hide them, neither the IDEA terminal nor from the system one AFAIK. `YellowBox.ignoreWarnings` and `console.disableYellowBox` only affect devices/simulators – lena Mar 28 '19 at 12:23

5 Answers5

7

I commented out the below lines and they stopped appearing in both terminal and the device.

node_modules/metro/src/lib/polyfills/require.js (Line 114)

  console.warn(
    "Require cycle: ".concat(cycle.join(" -> "), "\n\n") +
      "Require cycles are allowed, but can result in uninitialized values. " +
      "Consider refactoring to remove the need for a cycle."
  );

After commenting out that warning, restarting the project fixed my issues. PS. I'm using expo.

Thu San
  • 1,400
  • 1
  • 17
  • 29
5

My solution for this issue, hope it helps others:

import { AppRegistry, LogBox } from 'react-native';
import App from './App';
import { name as appName } from './app.json';

LogBox.ignoreLogs(['Require cycle:']);

AppRegistry.registerComponent(appName, () => App);
Phan Kiet
  • 91
  • 1
  • 3
0

I commented out the console.warn in node_modules/expo/build/environment/muteWarnings.fx.js Line 10.

console.warn = function warn(...args) {
    // if (args.length > 0 &&
    //     typeof args[0] === 'string' &&
    //     (/^Require cycle: .*node_modules/.test(args[0]) ||
    //         /Use UIManager\.getViewManagerConfig\('LottieAnimationView'\) instead\./.test(args[0]) ||
    //         /ReactNative\.NativeModules\.LottieAnimationView\.getConstants/.test(args[0]))) {
    //     return;
    // }
    // originalWarn.apply(console, args);
};
Squirrl
  • 4,909
  • 9
  • 47
  • 85
0

I went for a safer way :) I used patch-package to make sure I still see my own mistakes... mhm... require cycles :)

metro+0.59.0.patch

diff --git a/node_modules/metro/.DS_Store b/node_modules/metro/.DS_Store
new file mode 100644
index 0000000..21ab2f3
Binary files /dev/null and b/node_modules/metro/.DS_Store differ
diff --git a/node_modules/metro/src/lib/polyfills/require.js b/node_modules/metro/src/lib/polyfills/require.js
index 8c04756..3b208f6 100644
--- a/node_modules/metro/src/lib/polyfills/require.js
+++ b/node_modules/metro/src/lib/polyfills/require.js
@@ -114,11 +114,14 @@ function metroRequire(moduleId) {
         .map(id => (modules[id] ? modules[id].verboseName : "[unknown]")); // We want to show A -> B -> A:
 
       cycle.push(cycle[0]);
-      console.warn(
-        `Require cycle: ${cycle.join(" -> ")}\n\n` +
+      const cycleString = cycle?.join(" -> ");
+      if (!(cycleString?.startsWith('node_modules/'))) {
+        console.warn(
+          `Require cycle: ${cycleString}\n\n` +
           "Require cycles are allowed, but can result in uninitialized values. " +
           "Consider refactoring to remove the need for a cycle."
-      );
+        );
+      }
     }
   }
 
Stefan
  • 762
  • 1
  • 8
  • 11
0

Hiding Yellow boxes is not a solution, it's a kind of patch. You should look into your code actually there problem would be your components will be requiring each other. Like you make a component A which imports B and in B, you import A. If you please show your full logs, I'll further tell you the exact components.

STBox
  • 583
  • 8
  • 23
  • This is the correct way to go. Require cycles usually can be fixed joining modules or rearranging them. This post has a good and clear explanation of the problem: https://spin.atomicobject.com/2018/06/25/circular-dependencies-javascript/ – danb4r Aug 24 '23 at 23:39