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

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

Articles in series:
  1. Overview of NPM - How Did I Publish a Package on npm?
  2. Introduction to NPM - Building and Publishing Packages to NPM
Daily short news for you
  • Manus has officially opened its doors to all users. For those who don't know, this is a reporting tool (making waves) similar to OpenAI's Deep Research. Each day, you get 300 free Credits for research. Each research session consumes Credits depending on the complexity of the request. Oh, and they seem to have a program giving away free Credits. I personally saw 2000 when I logged in.

    I tried it out and compared it with the same command I used before on Deep Research, and the content was completely different. Manus reports more like writing essays compared to OpenAI, which uses bullet points and tables.

    Oh, after signing up, you have to enter your phone number for verification; if there's an error, just wait until the next day and try again.

    » Read more
  • I just found a quite interesting website talking about the memorable milestones in the history of the global Internet: Internet Artifacts

    Just from 1977 - when the Internet was still in the lab - look how much the Internet has developed now 🫣

    » Read more
  • Just thinking that a server "hiding" behind Cloudflare is safe, but that’s not necessarily true; nothing is absolutely safe in this Internet world. I invite you to read the article CloudFlair: Bypassing Cloudflare using Internet-wide scan data to see how the author discovered the IP address of the server that used Cloudflare.

    It's quite impressive, really; no matter what, there will always be those who strive for security and, conversely, those who specialize in exploiting vulnerabilities and... blogging 🤓

    » 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

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member 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...