Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Example: Node-API addon for Node.js and React Native

NotificationsYou must be signed in to change notification settings

callstackincubator/node-api-example-lib

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.

How to run the app

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

iOS

Run the iOS example app

npm run ios

Android

Follow 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 android
screencast.mp4

What's included?

  • A Node-API native module (namednode-api-example-lib)
    • CMakeLists.txt declares 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.cpp implements a simple Node-API addon exporting a "sum" function.
    • index.js the entrypoint of package, performing a singlerequire call to load the addon.
    • index.d.ts hand-crafted TypeScript declarations matching the addon implementation.
    • index.test.ts a test exercising the native addon using Jest via Node.js.
  • ./example-app Expo example app
    • App.tsx does the actual import ofnode-api-example-lib and providing a calculator UI.
    • package.json declaring dependencies on
      • node-api-example-lib: the example library in the root of the repository
      • react-native-node-api: the host package,
      • react-native in the restricted by the host package peer dependency,
    • babel.config.js adding thereact-native-node-api/babel-plugin plugin to transform require calls of Node-API.node addon files to calls into the host package.
    • metro.config.js needed to enable resolving thenode-api-example-lib from 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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp