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.
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:
For Node.js or browser: rollup-starter-lib.
For Vue.js 2: vue-sfc-rollup.
For React.js: react-component-library-starter.
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.
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.
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.
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!
Subscribe to receive new article notifications
Comments (0)