question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optional `require` doesn't work as expected

See original GitHub issue

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 label FAIL.

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:

ERROR  Error: Requiring unknown module "undefined". If you are sure the module exists, try restarting Metro. You may also want to run `yarn` or `npm install`. 

Am I missing something or the optional require is broken?

Configuration

  • Metro: 0.64.0(comes with RN 0.64.0)
  • Node: v15.11.0
  • yarn: 1.22.10
  • OS: MacOs BigSur 11.2.3 (20D91)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:9
  • Comments:6

github_iconTop GitHub Comments

1reaction
focuxcommented, Sep 4, 2021

I’m also experiencing the same, I’m using react native 0.64.1.

EDIT. I was able to fix this issue by changing inlineRequires: true to false in the metro.config.js file.

0reactions
smn-snklcommented, Apr 22, 2022

Running into the same issue. Did you ever solve this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

requireJS optional dependency - javascript - Stack Overflow
I've had exactly the same problem recently, and here's how I fixed it. I defined a RequireJS plugin called optional which ignores modules...
Read more >
RequireJS API
To load modules that are part of or use shim config, a normal require/define call is needed. Setting shim by itself does not...
Read more >
for - JavaScript | MDN - MDN Web Docs - Mozilla
The for statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, ...
Read more >
Module Methods - webpack
No CommonJS allowed, for example, you can't use require , module.exports or ... The ES2015 Loader spec defines import() as method to load...
Read more >
How to Deal with Optional Things and "Undefined" in TypeScript
Working with JavaScript means working with undefined . ... First, if you don't tell TypeScript that a property is optional, it will expect...
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