Reputation: 525
I have some code that I've written in a RN app, and I want to open-source and post it on npm for others to consume. What's the best workflow for writing a pure JS React Native library? Thanks!
Upvotes: 1
Views: 1745
Reputation: 4474
So these are the steps on how to create a re-usable React Native library and publish it to NPM:
npx create-react-native-library your-library-name-here
cd your-library-name-here
npm login
npm install --save react-native-builder-bob
npm install
npx react-native-builder-bob build
npm publish
You can verify at your https://npmjs.com home page that this library is now there. Now, to test it you have to create another React Native application, which will depend on the newly created library:
npx react-native init MyTestApp
cd MyTestApp
npx install --save your-library-name-here
App.js
main code entrypoint and make a call to a method in your library that multiplies two numbers to prove that the library works.npx react-native start
.npx react-native run-android
These steps needs a properly installed Android Studio IDE and node (Nodejs). The step to create the react-native library already has one callable pre-made test method called multiply(a, b)
.
Upvotes: 2
Reputation: 532
If your module is pure JS, you can simply follow these steps to publish to npm: https://docs.npmjs.com/getting-started/publishing-npm-packages
Essentially, you are exporting a component from your main file (index.js
). This should all be defined in your package.json
A RN example: https://github.com/ugiacoman/react-native-calendar
I'll be publishing this package to npm soon :)
If your module requires native code, you can use this generator to setup your project: https://github.com/frostney/react-native-create-library
Upvotes: 1