React-native: Require Non-JS Files

Created on 26 Mar 2015  路  12Comments  路  Source: facebook/react-native

I know there is discussion in other threads going on about webpack and having that would fix this issue.

However in the meantime there are quite a few NPM modules that are 100% compatible with the browser and would be with React-Native but they require JSON files for example and React seems to not like that.

Is there intention to add support for requiring arbitrary text files or JSON files? Similar to https://github.com/webpack/raw-loader

I guess what I am asking is for require hooks.

Thanks!

Locked

Most helpful comment

I'm not sure if this issue is solved for YAML files, but I wasn't able to require a YAML.

var yaml = require('../resources/i18n/message.yml')

This is what im doing to import the YAML, but I get an empty Object.

All 12 comments

yes, there are plans to give you the option to use webpack. Meanwhile add particular extensions, is json what you want? any others?

JSON would probably fix most issues.

An example library I was trying to use was.

https://github.com/markdown-it/markdown-it

It depends on an NPM module https://www.npmjs.com/package/entities which has an entities.json and due to that fails to work.

A temporary workaround for existing npm packages is to use browserify --standalone (or wzrd.in) or similar to generate a UMD bundle for the particular package you want to use, vendor it in to your project and then just require that file. I used this workaround successfully with htmlparser2.

However, ideally .json requires would be supported as they are supported out of the box in node, browserify and webpack, and that's the standard a lot of npm JS is written to.

@jsdf Thanks for the browserify --standalone workaround. This also worked for me.

@jsdf Could you provide a little more info about how you got it to work? I tried running htmlparser2 through wzrd.in and saving the result to htmlparser2.js then var htmlparser = require('./htmlparser2.js'); and I get an Requiring unknown module "../maps/decode.json" error.

@vjeux , are there any plans on updating the Common JS implementation for this project to act more like the standard? It seems a fairly common thing (including myself) to want something like the above.

I'm adding .json modules today :)

requireing .json landed internally, should land here next sync.

This is fixed, you can test it with:

var data = require('./package.json');
React.AlertIOS.alert(data.name);

I'm not sure if this issue is solved for YAML files, but I wasn't able to require a YAML.

var yaml = require('../resources/i18n/message.yml')

This is what im doing to import the YAML, but I get an empty Object.

@brentvatne get the same issue too. I can't read yml file

any plans to support yaml as well?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DreySkee picture DreySkee  路  3Comments

ghost picture ghost  路  3Comments

josev55 picture josev55  路  3Comments

aniss picture aniss  路  3Comments

axelg12 picture axelg12  路  3Comments