Juwan Wheatley
Juwan Wheatley

Reputation: 525

How do you start a React Native npm library?

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

Answers (2)

daparic
daparic

Reputation: 4474

So these are the steps on how to create a re-usable React Native library and publish it to NPM:

  1. Create an account at https://npmjs.com
  2. Open command line terminal, type:
    npx create-react-native-library your-library-name-here
  3. cd your-library-name-here
  4. npm login
  5. npm install --save react-native-builder-bob
  6. npm install
  7. npx react-native-builder-bob build
  8. 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:

  1. Open terminal
  2. npx react-native init MyTestApp
  3. cd MyTestApp
  4. npx install --save your-library-name-here
  5. Modify 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.
  6. Open another terminal, go to project root folder and type: npx react-native start.
  7. From the main terminal at step#1, type: 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

Ulises Giacoman
Ulises Giacoman

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

Related Questions