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.

Setting <Image /> source with string concatenation fails ('Requiring unknown module...')

See original GitHub issue

I’ve seen this mentioned before: https://github.com/facebook/react-native/issues/282#issuecomment-96908438

I’m trying to include an image from within the project, the name of which is dependant on variables.

var imageName = `image!${var1}_of_${var2}`
contents = (
  <Image source={require(imageName)} style={styles.card} onPress={this._onPress} />
)

The above fails with the usual:

Requiring unknown module “image!somwething_for_somethingelse”. If you are sure the module is there, try restarting the packager.

These also fail in the same manner:

<Image source={require(image!${var1}of${var2})}> <Image source={require('image!' + {var1} + '_of_' + {var2})} >

This works, no problem:

<Image source={require('image!somwething_for_somethingelse')}>

I’ve also tried

  • Stop and re-build Xcode project
  • Restarting the packager

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:20
  • Comments:31 (6 by maintainers)

github_iconTop GitHub Comments

146reactions
matejkramnycommented, Nov 29, 2015

this ‘feature’ is retarded and is annoying. Would very much be interested how i can bypass this stupid check

WHILE i agree that it is a good idea to stop developers from including images which consist of concatenated strings, it is the developer’s responsibility to code in such way that won’t cause him trouble in the future. A much better solution for react-native would be to print a notice to the console alerting them of what they just done.

113reactions
sahrenscommented, Sep 5, 2015

We intentionally don’t support dynamically generated image names because it makes it very hard to manage assets over time, just like how you wouldn’t want to do

var MyComponent = require(‘./’ + libName + typeOfComponent);

or something like that. Dynamic image name generation like this also won’t work with the new dynamic asset managing system we’re rolling out which let’s you add and update assets on the fly with just cmd+R (no more need to add to Xcode and recompile).

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native Image component, requiring unknown module
The answer is that you need to explicitly require all images or else they will not be bundled, hence the unknown module error....
Read more >
Output - webpack
The top-level output key contains a set of options instructing webpack on how and where it should output your bundles, assets, and anything...
Read more >
IO tools (text, CSV, HDF5, …) — pandas 1.5.2 documentation
If #1 fails, date_parser is called with all the columns concatenated row-wise into a single ... quoting : Set quoting rules as in...
Read more >
Content Configuration - Tailwind CSS
This guide covers everything you need to know to make sure Tailwind generates all of the CSS needed for your project. ​. Configuring...
Read more >
mod_ssl - Apache HTTP Server Version 2.4
This module provides SSL v3 and TLS v1.x support for the Apache HTTP Server. ... Such a file is simply the concatenation of...
Read more >

github_iconTop Related Medium Post

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