First, I would like to highlight some of the numerous benefits of publishing packages to the npm registry:

Prerequisites

The Business Proper: Private and Public Packages

I’d like to let us know that it’s possible to publish packages as either public or private packages on the npm registry.

Now, let’s jump into the business proper!

I need to choose a name for my package, it must not be something special, I mean it could be anything but bear in mind it must be unique.

Selecting a unique package name is crucial. To confirm the uniqueness of your proposed package name on npm, visit the npm website and search for your proposed package name, and that’s it!

Alternatively, you can confirm the uniqueness of a package name via the terminal by running this command npm search <your-package-name>

I have chosen fallback-avatar-hex as my package name because;

Initializing the Package

I have to run the below command to initialize my package: npx create-react-library fallback-avatar-hex

Then I was prompted to key in the letter y to proceed.

Then, I was prompted to answer a couple of questions. Thankfully, most of these questions are already pre-filled, you need to keep hitting the Enter key. However, you can change them if you wish. Use the arrow keys for the Package Manager and Template fields to choose your choices. It might take a little while to complete the whole process, just sit back, relax, and enjoy the show :)

Did you notice that I ran npx create-react-library fallback-avatar-hex and not npx create-react-app fallback-avatar-hex ?

This is why:

Using create-react-library offers several advantages. Firstly, it allows you to initialize your project for publication, providing you with an example app that facilitates package testing. Additionally, it sets up your project as a local Git repository, enabling easy integration with GitHub or any other version control system by simply adding the URL for your remote repository.

Below is what the structure of the app looks like when the create-react-library command is complete.

The src/ houses the ExampleComponent component which is like a placeholder for my FallbackAvatarHex component. The example/ houses the test app for testing the FallbackAvatarHex component.

The create-react library creates a React app in such a way that it has the example app and the main app in one React app, this is like a two-in-one React app.

Before adding my FallbackAvatarHex component, I’d like to show us see the scripts section of my package.json in the src/ dir.

Now, let’s cd into fallback-avatar-hex and start the dev server.

Fire up your terminal and run this command: cd fallback-avatar-hex && npm start

On a new terminal, run the below command: cd fallback-avatar-hex/example && npm start

The first command watches the src/ and recompiles it into the dist/ folder when you make changes, hence the start section of the scripts object in the src/package.json file.

The second command runs the example app that links to your package.

Adding My FallbackAvatarHex React Component

A close look at the src/ dir reveals an index.js which held the ExampleComponent component which is called in the App.js file of the example app.

I’ve got at least two ways to modify the index.js file, viz:

OR

I need to make sure that my component is being imported from the FallbackAvatarHex.js file and exported as well, so that any project that uses my package, will get to use my component accordingly. // src/index.js

NB:

Testing the Package

I need to ensure the package is working as expected before publishing it to the npm registry. To do this, I have to return my FallbackAvatarHex component in the App.js file of the example app like this:

The example app is currently running on port 3001, opening the app on http://localhost:3001, shows something like this:

Publishing My Package

First, I need to add/modify a couple of things before publishing my component to npm.

Then run npm run build, this optimizes and creates a production build for your package. I recommend running this every time you modify your package.

And finally, run npm publish, this command uploads your package to the npm registry. To confirm that it’s successful, navigate to https://www.npmjs.com/settings/your-username/packages to see all your published packages.

Alternatively, navigate to the npm website and search for the package name (fallback-avatar-hex in my case) using the search bar, and there you go :)

At this stage, you or anyone else can easily install and use the package on other projects/apps by running npm install --save fallback-avatar-hex or simply npm i fallback-avatar-hex

Remember to always increment the version each time you wish to publish again.


You can always reach me via:

Email: [email protected] LinkedIn: https://www.linkedin.com/in/jamesgozieodufu/

Merci pour votre temps!