This repo features a Node-API addon prebuilt for Node.js and React Native, built and loaded using the cmake-rn and react-native-node-api packages from the react-native-node-api repository.
Install dependencies (in the root)
npm install
Run the tests (using Jest via Node.js) (in the root)
npm test
Build the addon for React Native for the Android emulator and iOS simulator (in the root)
npm run build:rn
Change directory into the example app (the commands below should be executed from that directory there)
cd ./example-app
Install the example app dependencies
npm install
Generate the native build directories (./ios and ./android)
npx expo prebuild
Run the iOS example app
npm run ios
Follow the instructions on building React Native from source: More specifically, add these lines to the bottom of the android/settings.gradle file:
// ...
include ':app'
includeBuild('../node_modules/@react-native/gradle-plugin')
+ includeBuild('../node_modules/react-native') {
+ dependencySubstitution {
+ substitute(module("com.facebook.react:react-android")).using(project(":packages:react-native:ReactAndroid"))
+ substitute(module("com.facebook.react:react-native")).using(project(":packages:react-native:ReactAndroid"))
+ substitute(module("com.facebook.react:hermes-android")).using(project(":packages:react-native:ReactAndroid:hermes-engine"))
+ substitute(module("com.facebook.react:hermes-engine")).using(project(":packages:react-native:ReactAndroid:hermes-engine"))
+ }
+ }Manually set the REACT_NATIVE_OVERRIDE_HERMES_DIR environment variable (in ./example-app - required only for the Android app to build)
export REACT_NATIVE_OVERRIDE_HERMES_DIR=`npx react-native-node-api vendor-hermes --silent`
Run the Android example app
npm run android
screencast.mp4
- A Node-API native module (named
node-api-example-lib)CMakeLists.txtdeclares the configuration for the CMake meta-build system, used to build the native module using bothcmake-js(for Node.js) andcmake-rn(for React Native - iOS and Android).addon.cppimplements a simple Node-API addon exporting a "sum" function.index.jsthe entrypoint of package, performing a singlerequirecall to load the addon.index.d.tshand-crafted TypeScript declarations matching the addon implementation.index.test.tsa test exercising the native addon using Jest via Node.js.
./example-appExpo example appApp.tsxdoes the actual import ofnode-api-example-liband providing a calculator UI.package.jsondeclaring dependencies onnode-api-example-lib: the example library in the root of the repositoryreact-native-node-api: the host package,react-nativein the restricted by the host package peer dependency,
babel.config.jsadding thereact-native-node-api/babel-pluginplugin to transform require calls of Node-API.nodeaddon files to calls into the host package.metro.config.jsneeded to enable resolving thenode-api-example-libfrom the parent directory of the app.
Note
This example doesn't handle the complexity of distributing prebuilds for Node.js targeting multiple operating systems and architectures. See tools like https://www.npmjs.com/package/prebuild and https://www.npmjs.com/package/@mapbox/node-pre-gyp for solutions to this challenge.