Scaffold a React Native library
If you want to create your own React Native library, scaffolding the project can be a daunting task.
create-react-native-library can scaffold a new project for you with all the necessary tools configured.
- Minimal boilerplate for libraries on which you can build upon
- Example React Native app to test your library code
- TypeScript (opens in a new tab) to ensure type-safe code and better DX
- Support for Turbo Modules (opens in a new tab) & Fabric (opens in a new tab)
- Support for Kotlin (opens in a new tab) on Android & Swift (opens in a new tab) on iOS
- Support for C++ to write cross-platform native code
- Expo (opens in a new tab) for libraries without native code and web support
- ESLint (opens in a new tab), Prettier (opens in a new tab), TypeScript (opens in a new tab), Lefthook (opens in a new tab) and Release It (opens in a new tab) pre-configured
react-native-builder-bobpre-configured to compile your files
- GitHub Actions (opens in a new tab) pre-configured to run tests and lint on the CI
To create new project, run the following:
npx create-react-native-library@latest awesome-library
This will ask you a few questions about your project and generate a new project in a folder named
After the project is created, you can find the development workflow in the generated
While the default templates are for libraries that are published to npm, you can also create a local library that is not published but used locally in your app.
You'd typically use a local library when:
- You're building a native library for your app and don't want to publish it to npm.
- You want to be able to easily copy the library to other projects.
- You're in a monorepo and want to keep the library code in the same repository as the app.
- You're using Expo, but want to use vanilla React Native API for native modules and components.
The structure of the app with a local library may look like this:
├── modules <-- folder for your local libraries
│ └── awesome-library <-- your local library
If you run
create-react-native-library in an existing project containing a
package.json, it'll be automatically detected and you'll be asked if you want to create a local library. You can also pass the
--local flag to the command to explicitly create a local library:
npx create-react-native-library@latest awesome-library --local
The local library is created outside of the
ios folders and makes use of autolinking to integrate with your app. It also doesn't include a separate example app and additional dependencies that are configured in the default templates. This is an alternative approach to the setup mentioned in React Native docs (opens in a new tab).
The advantages of this approach are:
- It's easier to upgrade React Native as you don't need to worry about custom code in
- It can be used with Expo (opens in a new tab) managed projects with custom development client.
- It's easier to copy the library to other projects or publish later if needed.
- The boilerplate for the library doesn't need to be written from scratch.
- It can be used with monorepos where the additional tooling in the default templates may not be needed.
This creates a symlink to the library under
node_modules which makes autolinking work. But if you're using a monorepo or have non-standard setup, you'll need to manually set up the package to be linked to your app based on your project setup.
Once the project is created, you can follow the official React Native docs to learn the API for writing native modules and components: