Overview of NPM - How Did I Publish a Package on npm?

Overview of NPM - How Did I Publish a Package on npm?

Daily short news for you
  • Dedicating to those who may not know, snapdrop is an open-source application that helps share data with each other if they are on the same Wifi network, operating based on WebRTC.

    Why not just use AirDrop or Nearby Share instead of going through this hassle? Well, that’s only if both parties are using the same operating system. Otherwise, like between Android and iOS, or MacOS and Linux or Windows... At this point, snapdrop proves to be extremely useful 😁

    » Read more
  • 24 interesting facts about SQLite Collection of insane and fun facts about SQLite. I have to say it's really amazing 🔥

    » Read more
  • bolt.new is so awesome, everyone! It can handle all types of Front-end work. The thing is, I was out of ideas, so I went there and described in detail the interface I wanted, the more detailed the better, and it just generated it for me, which was surprising. Although it's not perfect, it's very similar to what I imagined in my head, and making adjustments is easy. By default, this tool generates code for React or something, so if you don't want that, just ask it to generate code for the platform you're using.

    The downside is that each subsequent prompt will replace all the old code -> it consumes tokens. You probably can only request a few times before running out of the free tokens for the day. The upside is they are currently giving away 2 million free tokens for everyone. Go get yours now 🥳

    » Read more

Introduction

I'm sure that everyone here has heard of, knows, and uses npm daily in their work. NPM is a huge repository of packages, mostly JavaScript libraries used for web development or Node.js. But where do these packages come from? The answer is the community, which includes you - those who have interesting ideas and are willing to share.

After using various packages, I encountered a problem - I couldn't find any component to input OTP for Vue.js at that time (now I'm not sure either :D). As a result, I had to write my own. Instead of using it alone, why not package it and share it online? That's when I thought of the "magical distribution network", npm.

The idea of building a package always arises everywhere, even though there may be dozens or even hundreds of different packages solving the same problem. It's challenging to compete with well-established packages that have a huge number of downloads. However, even a small contribution is still a contribution, right? What you need is to create a highly reliable package, release it at the right time, and have a bit of luck.

Choosing a Platform for Packages

There is a wide variety of packages on npm. For example, there are packages exclusively for browsers or exclusively for Node.js, as well as packages that support both. Also, a package written for Vue.js cannot be used with React.js, and vice versa.

Therefore, it's important to determine which platform you are going to write the package for. For example, I chose to create a package called "OTP Input for Vue.js". Building a package is easier if you find a suitable "framework". Here, a framework refers to a project that has everything set up, from building, development, testing, and so on. Your task is just to focus on the logic. Alternatively, if you don't find or don't want to use an existing framework, you can choose to build from scratch, which means you have to set up everything. Of course, you can omit some components such as development or testing, but that will make your package less "professional".

Here are some popular frameworks that I know of:

After completing the logic for the package, you need to create a README.md file that describes how to use it, its attributes, and any other necessary information for others to understand. Additionally, provide instructions for running development, tests, and contributions. The final step is publishing the package on npm.

Getting Familiar with the npm CLI

The npm CLI helps me push the package to the repository. By default, when installing Node.js, npm is also installed, so if you don't have npm yet, install Node.js!

Check the npm version:

$ npm -v
6.14.12

Log in to npm:

$ npm login

Navigate to the directory containing the project you want to publish, for example, here I move to the vue-otp-2 directory:

$ cd vue-otp-2

Ensure that the package.json file is present in that directory. npm relies on package.json to publish your package, and a basic package.json file must contain the name and version:

{
  "name": "vue-otp-2",  
  "version": "1.0.0",  
}

Here, name is the package name used for installing (npm i vue-otp-2), and version is the package version at the time of publishing.

Then publish it:

$ npm publish

Wait until the publishing process is successful, and you will receive an email notification about it.

Some notes on publishing:

  • Make sure your package version follows the semantic standard. If you're not familiar with it, you can learn more here.

  • Make sure the package name is unique on npm. For example, you cannot name your package vue-opt-2 because I already own that name. However, if you still want to use that name, npm provides an option to add your username to the package name, something like @hoaitx:vue-opt-2. See how to set it up here.

  • Besides the name and version, there are many other interesting parameters to make your package profile look "cool". Learn more here.

Tips & tricks:

  • Some people have successfully published their packages but couldn't use them when installing in a project. This might be because they didn't specify the correct entry point module in the package. Specifically, package.json has a property called main, which is used to determine which file to import when using require/import. By default, it is index.js. For example, if a package named foo is imported with require("foo"), it corresponds to the path node_modules/foo/index.js.

  • Try on RunKit is an application that allows you to test packages before deciding whether to install them :D.

Conclusion

In this article, I've briefly described the process of publishing my package on npm. In the next article, I will go into detail on each step of creating a package. Oh, for those who have been wondering why there hasn't been a link to my package from the beginning until now, here it is: vue-otp-2.

Premium
Hello

5 profound lessons

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...
Scroll or click to go to the next page