2

Requiring image with string variable doesn't work:

// working
var texture = require('./images/imag_01.jpg');


// doesn't work
path = './images/imag_01.jpg';
var texture = require(path);

// Error: Cannot find module "."

3 Answers 3

3

require doesn't accept a variable.

But you can do so with a dynamic import() statement should you need to import dynamically, if you are using ES6.

* Update: 2018.11.19 *

React v16.6 introduced React.lazy, which you can use to split code more easily.

Sign up to request clarification or add additional context in comments.

Comments

3

It is not your fault, it is how it is, unfortunatetly, supposed to work. You must write require('image-path') and cannot have a variable inside the parenthesis.

See: https://facebook.github.io/react-native/docs/images.html

Comments

0

This worked for me:

var image_name = 'imag_01.jpg';
var texture = require('./images/'+image_name );

1 Comment

it returns' Cannot find module './undefined''

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.