How to Publish a Custom React Hook as an NPM Library
This lesson will teach you how to publish a React hook as an NPM library.
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the The newline Guide to Full Stack Comments with Hasura and React course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to The newline Guide to Full Stack Comments with Hasura and React, plus 70+ \newline books, guides and courses with the \newline Pro subscription.
In the previous modules, we added a commenting system to a sample block application. But what if we have multiple applications that we want to add comments to? We would need to copy paste the hook to different projects. However, we can also publish the hook as an NPM library and import it to any project. That's what we'll do in this lesson and it's going to only take a few steps. To deploy our hook as an NPM package, we need to use a bundler. Module bundler is a tool that bundles JavaScript or TypeScript modules into a single JavaScript file that can be executed in the browser. We're going to use Micro Bundle, which is a zero configuration bundler for tiny modules. It comes with TypeScript support out of the box and it's pretty straightforward to set up. We can install it with the following comment. Before we use it, let's fill a few things in the package JSON. We need to add the name and set it to how we want our library to be called. I will propend it with my NPM username, but feel free to use whatever name you wish. Then source, we need to point where is the source code. Then we have main, the location where Micro Bundle should generate the command JS bundle. Types, TypeScript Types location. Micro Bundle will generate types from our module and put them there. The next thing is module. The location where the ESM bundle should be placed. Files, files that should be included in the package. We want to ignore all the block application code and only include our hook code . We also need to set a version for our package and then set private to false so that we are able to publish the library. Since we are publishing the package from the same place where we have the block application, we will add a separate TS config specifically for the library. It will extend our main config and add a few more options. The first one is out there in compiler options. We will set it to this and we will also add include key and we will set it to the use command source code. Then we will add a new script to the package JSON that will compare the source code to this directory. We will call Micro Bundle without compressing and we will pass our newly created TS config. Now we can try and run it with the following command. Yarn built Micro Bundle. You should see the similar output. If you want to use the use commands in an application, it needs to have React installed. However, we don't want to install React along with the hook installation. Instead, we need a way to say that it only works if there is a React dependency . We can do it by specifying the pure dependencies in package JSON. By that, we are saying that the coded install's our package must have React in version 16.8 or greater. If you don't have an npm account, you can create one on an npm website. After creating the account, you can log into npm via the command line with npm login command. Then it will ask you to provide your credentials as your username and password. To make sure that you are logged in correctly, you can run npm who am I. After completing the steps, we are ready to publish the package with this single command such as npm publish. After running this command, you can check your npm account and see if the package is there. You can now use the use commands hook from the newly published library. I promise that it will only be a few steps and I hope that it all went smoothly for you. In the next lesson, we'll look into one more hook customization. We'll make commands to require manual approval.