0

I was working on a React Native Native Module for Android. And it is written in Java (as what I researched from official docs and the internet). The problem is, I need to use a Node.JS based module to implement the functions that I want. Is there any possibility to use Node.JS module in Java?

The Node.JS module currently using a few Node native libraries such as fs and path. The code is 100% written using Node.JS.

Or is there any other way to create a React Native module without the use of Java?

The ultimate goal is to use this Node.JS based module to React Native App (without create a bridge between Node and React Native).

0

1 Answer 1

0

As I understand your question, your module doesn't have to communicate with native side. If it's that what you need, it will so easy to achieve. I'll make a simple example:

yourModule.js

export function a() {
    // do sth
}

export function b() {
    // do sth
}

export function c() {
    // do sth
}

yourComponent.js

There is 2 ways to use your module:

1.

import { a, b } from '/path/yourModule'; // if you want to import some of the functions

class yourComponent {
    function test() {
        a(); // call function a from module
    }
}
// Remember to check function's name if it's duplicated with other modules

2.

import * as module1 from '/path/yourModule';

class yourComponent {
    function test() {
        module1.a(); // call function a from module
    }
}
// In this case, you won't have to worry about duplicated function's name, but you still
// have to check duplicated of the name of modules you have imported. For this example,
// the name module1 has been used.

P/s: you might get some error when using export/import incorrectly. So please investigate more about React Native export/import.

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

7 Comments

Hey sorry for the late reply, let me clarify something. Now I have a Node.JS library and a React Native App, I want to use this Node.JS library inside the RN App and we thought of creating a RN Module based on this Node.JS library so that the RN App could use the RN Module without the needs of integrating with Node.
Thus, now the problem is RN Module is using Java, I need to import the Node.JS library and use the functions inside but I could not find a way.
So, you mean that: you're using a NodeJS library, but it uses Java code to execute in the native side and itsn't for React Native? If that is your problem, there is only one thing you can do: make a native module for RN that does the same thing your NodeJS module does. Basically, RN App has 2 threads: JS thread and Native thread. JS thread will execute your JS code, and then send the datas and commands to your Native thread. Your Native thread will receive those, and work like a normal app on the phone. Simply, JS thread is the brain and Native thread is the muscles.
First, Kotlin is fully compatible with Java, and it can be converted to Java. Actually, I've never used Kotlin to write RN native module, but you can try this Writing RN android module in Kotlin.
Second, since Kotlin is likely an extended version of Java, there is no way that the code you import would run. In Native Modules Android, you will realize that it's a bit different from usual Android app. Every native modules you write must follow RN's rules, e.g: extend ReactContextBaseJavaModule instead of Activity. Even in your NodeJS module, the Native code has to follow its own rules. All in all, if you can't find an alternative libraries for RN that do the same thing, you have to do it by yourself.
|

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.