Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to require a non-image asset in react-native new asset system?

In react-native 0.14, we can require an image using const fireImage = require('./images/demo.jpg'). The require works fine with images(png, jpeg, gif), 'js' and 'json' files.

My question is: Is it possible to 'require' a '.mp3' or '.mp4' files?

I have tried the 'mp3' file and the Xcode reported a build error like this

uncaught error Error: UnableToResolveError: Unable to resolve module ./music/some from /Users/rongshen/tmp/react-native-test/imageTest/index.ios.js: Invalid directory /Users/rongshen/tmp/react-native-test/imageTest/music/some.mp3
like image 431
user552403 Avatar asked Oct 23 '25 23:10

user552403


1 Answers

For anyone else running across this post that's still trying to figure it out, you can utilize the rn-cli.config.js file to add additional file types for the require() method.

Example, add a file named rn-cli.config.js to the project root with the following contents:

module.exports = {
    getAssetExts() {
        return ['mp3', 'mp4']
    }
}
like image 180
shparkison Avatar answered Oct 25 '25 11:10

shparkison



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!