40

Is it possible to use npm modules with React Native projects directly, like one uses them within a React project by npm install <module-name>?

Of course I mean modules that can be used with a React app, that is front-end ones that will be run in the browsers JS runtime but not in the nodejs or iojs runtime as a React Native app does not run in the nodejs or iojs runtime.

1
  • 2
    now it's 2017, any update?
    – NamNamNam
    Dec 20, 2017 at 9:31

1 Answer 1

40

Well, it's quite opposite. React Native actually runs within io.js runtime so most pure javascript modules for node will work. On the other hand most front-end modules written for React.js will not work for React-Native.

React Native does not use HTML DOM nor CSS as we know it from the web. It replaces the CSS/HTML DOM with the native view representation. So any front-end packages that are supposed to use HTML and be displayed in browser will not work.

On the other hand, any modules that are pure javascript and run within node.js/io.js are perfectly OK to be run in react-native.

For example, I am quite sure that Facebook uses their 'relay' data access library in their react-native apps (it's a javascript library that efficiently communicates over Facebook's Open Graph API and allows to access Facebook user's data).

The way to do it is the same as in other node.js/io.js apps. Simply run

npm install module --save

and you are done (package.json will be automatically update with the dependency for the module). Then you can use the package as usual.

6
  • By front end libs I meant js, without HTML, css ones like superagent, socket.io etc. Excluded http servers and wrappers of sort like Koa, express, since they run in iojs runtime. But if a react native app on a mobile device runs in a iojs runtime, js parts, and which I think not, backend ones will also. But that sounds strange :)
    – sçuçu
    Aug 30, 2015 at 12:38
  • OK. I see, front-end for me is always HTML :). I know node is mostly used to run webservers, but in fact node (and io.js) is much more than that. It's a runtime for any kind of event-driven application - see more here: javaworld.com/article/2079190/scripting-jvm-languages/… . It does not have to be web server, and from what I know about react native is that it's using io.js as runtime environment - for sure it users iojs's require, modules, npm packager and plenty other components from iojs (that's what I understand by runtime). Aug 30, 2015 at 17:59
  • 4
    React native uses the JavaScriptCore runtime not iojs. So node_modules using in-built node_modules like stream, fs, etc wont work
    – saby
    Oct 16, 2016 at 10:21
  • @saby maybe it has changed from iojs to JavaScriptCore in between the time Jarek commented, has it?
    – sçuçu
    Feb 28, 2017 at 10:36
  • Can we use js Tesseract OCR package for React Native?
    – Fiju
    Dec 13, 2017 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.