Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the role of Node.js in React-Native?

I'm trying to understand the development process of React-Native, so I've found information about Metro, And then I've read/watch this Metro video):

Metro is the development platform for React Native and it does that by exposing an HTTP server so clients, in this case, emulators can communicate with it and it also exposes a Websocket server so it can push updates into the clients.

The docs talk about the "React Native Packager" (now called Metro, according to the video) which runs on port 8081, so that is the HTTP server that starts when we type react-native run-android for example?

Regarding the Websocket I still need to read more.

The documentation says we're running our JavaScript code in two environments, depending if we're in debug mode or not, which I understood. But this article confused me a little bit, says:

No. 4 You Code Does Not Run on Node.JS: The JavaScript runtime you’ve got is ether JavaScriptCore (non-debug) or V8 (debug). Even though you can use NPM and a node server is running on the background, your code does not actually run on Node.JS. So you won’t be able to use of the Node.JS packages. A typical example is jsonwebtoken, which uses NodeJS’s crypto module.

And, then I've read things like:

React Native uses Node.js, a JavaScript runtime, to build your JavaScript code.

Node.js is a server-side JavaScript runtime environment. React Native ships with some tools that are written for Node.js.

Node.js is an open source platform built on Chrome's JavaScript runtime; it offers a way to easily build fast, scalable programs. Node.js allows you to run JavaScript in Terminal, and helps create modules.

In this article, it says:

Download node.js from nodejs.org. This JavaScript runtime gives you access to npm, which is a convenient tool created by the node.js project that you can use to manage open source packages. Make sure that you download the latest LTS (Long Term Support) version of node.js. Also included with this download is a development server called the Metro bundler, which provides live updates when debugging.

So:

  • The role of Node.js in RN is to only access npm and manage the packages? and is Metro is includes in Node.js? Am I missing/confusing something? Thank you.
like image 511
Ana Avatar asked Mar 17 '19 12:03

Ana


People also ask

Is Node JS necessary for React Native?

Developers typically assume that for running Reactjs, they have to need the Nodejs backend. However, it is not true; you DO NOT need to have Nodejs every time you use React. Understand Reactjs is a library used to render the user-interfaces (UI) of your web as well as your mobile apps.

Is Node JS same as React Native?

What is React Native? Just like Node JS, React Native it is written on JavaScript. However, where Node JS is a complete environment, React Native is a cross-platform framework. React Native is based on Facebook's React project, and the company intends to pursue this framework further.

Can React Native run Node JS?

Using this package you can: run http servers in Android, use Node streams, interface with the filesystem, off load some heavy processing out of the JS thread in React Native, and more! Running the real Node. js in Android, you can do everything that Node. js on desktop can.

What is the purpose of Node JS?

Node. js is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. It's used for traditional web sites and back-end API services, but was designed with real-time, push-based architectures in mind.


1 Answers

There are four types of JavaScript you'll write in todays environments:

1) Clientside browser JavaScript:

That's what gets sent to webbrowsers when they visit your webpage, it then gets executed in the browser at the clientside. As you want the JS to load fast and run on all kinds of browsers, you usually use transpilers to turn the modern ESnext you write into a minified version with better support.

2) Clientside native JavaScript:

Most devices do have a native JS runtime, therefore you can ship JS files with your Android / iOS / Desktop application and then start them there. These engines also support adding hooks from JavaScript into your native code, that's how React Native does provide it's APIs.

3) Serverside NodeJS JavaScript:

NodeJS is a runtime you'll use to run servers.

4) Buildscripts running on NodeJS:

You can use JavaScript to generate JavaScript files. That's how you bundle the files for (1) and (2) (maybe also (3)).

Now metro is a serverside buildscript (on NodeJS) that you can use to either a) start a server that serves your JS as a webpage (1 & 3), or b) that bundles your JS in a native App that you can install on your device (2).

The role of Node.js in RN is to only access npm and manage the packages?

No. metro is itself a package that you then run on NodeJS.

like image 110
Jonas Wilms Avatar answered Oct 21 '22 12:10

Jonas Wilms