- Notifications
You must be signed in to change notification settings - Fork2
callstackincubator/node-api-example-lib
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This repo features a Node-API addon prebuilt for Node.js and React Native, built and loaded using thecmake-rn andreact-native-node-api packages from thereact-native-node-api repository.
Install dependencies (in the root)
npm installRun the tests (using Jest via Node.js) (in the root)
npm testBuild the addon for React Native for the Android emulator and iOS simulator (in the root)
npm run build:rnChange directory into the example app (the commands below should be executed from that directory there)
cd ./example-appInstall the example app dependencies
npm installGenerate the native build directories (./ios and./android)
npx expo prebuildRun the iOS example app
npm run iosFollow the instructions onbuilding React Native from source: More specifically, add these lines to the bottom of theandroid/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 theREACT_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 androidscreencast.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 likehttps://www.npmjs.com/package/prebuild andhttps://www.npmjs.com/package/@mapbox/node-pre-gyp for solutions to this challenge.
About
Example: Node-API addon for Node.js and React Native
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Contributors2
Uh oh!
There was an error while loading.Please reload this page.