-
Notifications
You must be signed in to change notification settings - Fork 626
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
Optional require
doesn't work as expected
#666
Comments
require
doesn't work as expectedrequire
doesn't work as expected
Any update on this @karol-bisztyga ? try {
await import('./does-not-exist.json');
} catch {
console.warn('NON EXISTING');
} Gives me try {
require('./does-not-exist.json');
} catch {
console.warn('NON EXISTING');
} Gives me:
|
I'm experiencing the same problem in my project built with React Native (and Metro) 0.64.2. |
I'm also experiencing the same, I'm using react native 0.64.1. EDIT. |
@focux Even with this config |
I have tried to use babel-plugin-optional-require (https://github.com/satya164/babel-plugin-optional-require) with success when the module don't exists but without success when the module exists. |
Running into the same issue. Did you ever solve this? |
I too have been unsuccessful in loading an optional try {
require( './setup-local' ).default();
} catch ( _error ) {} ...results in errors like the following...
However, I found a workaround relying upon the As an example, the following searches for a const req = require.context("./", false, /setup-local\.js$/);
req.keys().forEach((key) => req(key).default()); |
Do you want to request a feature or report a bug?
Report a bug
Current behavior
I wanted to implement an optional
require
that was discussed here. It's said that this functionality has been added in metro 59.0, PR. RN 0.64.0 uses @react-native-community/cli version 5.0.1-alpha.0(here) and that uses metro version 0.64.0(here) so that change should be included there.Ok, there's a stack trace but still, I don't think this functionality works as expected.
Reproducing the issue
I just installed an RN app with this commend:
npx react-native init appName --version 0.64.0
and added some js code to test the thing.Reproducing repository
Here is the repository.
Clone the repo I posted, install the packages(
yarn && cd ios && pod install
), run the app, and press the button with the labelFAIL
.Expected behavior
Running the app from the repro I linked, I still get the Redbox. I want to be able to handle a lack of files in
require
on my own providing a specific error message with instructions on what can be done(or do whatever I want with this basically). Instead, I get the Redbox with something like this:Am I missing something or the optional
require
is broken?Configuration
0.64.0
(comes with RN0.64.0
)v15.11.0
1.22.10
MacOs BigSur 11.2.3 (20D91)
The text was updated successfully, but these errors were encountered: