-
Notifications
You must be signed in to change notification settings - Fork 635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Require cycle warnings should be opt-in when the source is node_modules #287
Comments
Any workaround for this issue? |
I get the warn as well. I've tried to ignore this but it didn't help me.
|
I can't deal with these warnings right now in my app. I mute them via a npm postintall script including this mess:
|
Would love to be able to opt-in to this warning. In the meantime, similar to @defualt, I've used patch-package to apply the below patch to ignore the warning only if its an external dependency: Patchpatch-package
--- a/node_modules/metro/src/lib/polyfills/require.js
+++ b/node_modules/metro/src/lib/polyfills/require.js
@@ -111,21 +111,29 @@ function metroRequire(moduleId) {
var initializingIndex = initializingModuleIds.indexOf(
moduleIdReallyIsNumber
);
+
if (initializingIndex !== -1) {
var cycle = initializingModuleIds
.slice(initializingIndex)
.map(function(id) {
return modules[id].verboseName;
});
- // We want to show A -> B -> A:
- cycle.push(cycle[0]);
- console.warn(
- "Require cycle: " +
- cycle.join(" -> ") +
- "\n\n" +
- "Require cycles are allowed, but can result in uninitialized values. " +
- "Consider refactoring to remove the need for a cycle."
- );
+
+ var isExternalOnly = cycle.every(function(cycleWarning) {
+ return cycleWarning.includes("node_modules")
+ })
+
+ if (!isExternalOnly) {
+ // We want to show A -> B -> A:
+ cycle.push(cycle[0]);
+ console.warn(
+ "Require cycle: " +
+ cycle.join(" -> ") +
+ "\n\n" +
+ "Require cycles are allowed, but can result in uninitialized values. " +
+ "Consider refactoring to remove the need for a cycle."
+ );
+ }
}
} |
@codybrouwers I have tried to use your patch but I received the following error from
|
@toioski Ah yes, I've encountered that issue with patch-package as well. Better to just make the same changes and patch it yourself I think. |
I'd like to add that require cycle may be intentional inside your app code and is fine in many cases if you know what you are doing and don't start calling cycle methods on initialization . I'd like to be able to disable those warnings even for my own code, is there a way? |
@slorber In those cases the warning should be displayed. The problem like the OP said is:
|
Hi! We added this warning since we got many complains from developers at FB that it was hard to debug the errors caused by cyclic dependencies (since a cyclic dependency causes the returned value of a The YellowBox.ignoreWarnings([
'Require cycle:',
]); |
@rafeca yes, immediately after |
Today I updated versions to:
And I became to get new warning
It doesn't work as well. |
This is so annoying, any updates here? Every time I load the app I get like a million require cycle warnings which I really don't care about and muting them as suggested doesn't work. Is there any other way of silencing this? |
Found workaround here: facebook/metro#287 (comment)
Here is my index.js:
|
|
IMO this should be opt-in, not opt out. Many people also don't understand what it is and think they need to fix this warning even if nothing is wrong with the code. There are also third-party libs with the warning that you can do nothing about. Also to find require cycles, you have to go through hundreds of warnings, doesn't seem very efficient to find an issue. Enabling/disabling all of the warnings don't seem ideal either, it'll probably be better to enable this per folder rather than for whole project. |
Found workaround here: facebook/metro#287 (comment)
Okaaaay, so if this warning is annoying the hell out of you, and you can't refactor your app because it's too much work, here's how I "suppressed" those warnings until a proper solution shows up :
const fs = require('fs');
const codeToObscure = /console.warn\([\s\S].*"Require cycle: "/;
const problemFilePath = './node_modules/metro/src/lib/polyfills/require.js';
const problemFileContent = fs.readFileSync(problemFilePath,'utf8');
fs.writeFileSync(problemFilePath,problemFileContent.replace(codeToObscure,'const noConsoleWarn = (""'),'utf8');
When it's done, just This is really really tricky for us since we have LOTS of require cycles because of how we use redux in our app. I hope this is helpful. |
Found workaround here: facebook/metro#287 (comment)
Ok here's another hack, but this is rather good. UNIX comes to the rescue, guys. Use grep!! Define your ignore patterns in a file. Here's mine:
Run Metro like this: This will work, but will also cause the terminal output to lose its colors. The reason is that since the Metro output is not going directly to the terminal anymore but to the
See the relevant section in Chalk docs for info on Also if you are a big nerd see the script Linux command for a more generic way of doing this, one that will work in every scenario. I know this is not the fix you're looking for, but to me this is less fragile than the other hacks mentioned above. |
Seems that in new versions of metro, the require cycle message uses ` instead of " so your regex breaks. I've modified your code to this in my project, should work with both " and ` (only tested with `):
|
Yes, here in my project disappeared this logs in the console. Thanks!! 👍 |
I find it hard to believe that 2 years later we still can't silence the console warnings (not |
Guys ... how is this still a problem? Can we please just wrap this logic in a bloody env var and be done with it? |
This worked for me. Absolute madness that we have to do this. But I really appreciate the collective work on a community solution 💪🏽. |
beautiful dev peops, heads up when upgrading to react-native |
I use: LogBox.ignoreLogs(['Require cycle:']) Right after imports, in App.js; notice this is for newest versions of react native. |
Afaik, in react-native 0.64.0, LogBox doesn't affect the output of metro cli itself, only what is logged by the loaded bundle at runtime. Metro is resolving imports and logs this message regardless of what is configured in LogBox. |
I'm using expo sdk 41 and this is working. |
Still have the same issue. Tried to use like below but not worked for me on LogBox.ignoreLogs(['Require cycle:']) And I actually don't want to ignore this error but mostly getting this because of absolute path usage. Is there a proper way to use absolute path without getting this kind of warnings? |
Anyone knows how to disable warnings in Metro server output (after react-native start)?
LogBox doesn't work |
Could you replace that with currentlyFocusedInput? I'm guessing you are using an old version of react navigation, you can use npm patch to create a patch for a node_module disabling the warning will be a problem, because it says it will be removed in a future version. If you could fix it please send the patch so other people could use it too. |
I've been looking for a solution for a month now and nothing works! I think I'll stop the code |
Try to refractor your code, and use globalThis where it helps. (if it in your project if warning is in node_modules try to patch it and inform the developers) |
Now I want a shirt that reads: |
That'll work as well, no more warnings or errors ever again. |
NEVER |
Require cycle: node_modules/react-native/Libraries/Network/fetch.js -> node_modules/whatwg-fetch/dist/fetch.umd.js -> node_modules/react-native/Libraries/Network/fetch.js |
That warning of yours comes from the react native itself, I'm on latest version and have not this problem, maybe consider upgrading? |
I just created a PR for a solution that should be a bit more flexible, allowing users of Metro to suppress certain libraries' warnings. This ensures that you can still see require cycle warnings for new modules that you've installed (so you can decide for yourself if they're an issue). If anyone is interested in the fix, take a look at #707 |
Wow! 4 years now! Can you PLEASE make this optional. :) Thank you! Can't believe "Facebook Engineers" need this type of stuff to debug their apps. |
Hi @carlos-dubon, thanks for the contribution. This was addressed in #707 and released in Metro 0.70.2 - |
They don't. It's just how corporations work. Usually it goes like this: Managers: Why do our apps suck so much?? Some engineer implements require cycle detection, gets promoted to E7. The new warning is useless and annoying everyone. Apps still suck. Everyone loses. |
FWIW, dependency cycles prevent topological ordering, which can be a problem for granular, distributed build systems, as well as potentially introducing surprising runtime behaviour. The warning isn't useless, but it was too noisy in OSS - mainly due to being unactionable under |
Do you want to request a feature or report a bug?
Feature
What is the current behavior?
Lots of warnings about require cycles from libraries in node_modules in the react-native ecosystem, coming from the following code:
metro/packages/metro/src/lib/polyfills/require.js
Lines 158 to 174 in dc8d859
What is the expected behavior?
Don't warn users about require cycles in libraries in node_modules unless the user opts-in. Warnings about code that you don't own, especially when the warnings are non-essential, create unnecessary noise.
Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system.
React Native 0.57 default configuration and Metro version (0.47.1). Other information irrelevant.
The text was updated successfully, but these errors were encountered: