Skip to main content

How to publish your component to npm

ยท 3 min read

The front-end components are developed in javascript, packaged in Node.js and published to npm. So we want to do component release, we need to understand the development and release of npm package first.

npm package project initializationโ€‹

We often use the npm init command to initialize node projects.

If the default settings are used, you can add the -y parameter. now we create a new npm-components folder and initialize the project:

$ mkdir npm-components
$ cd npm-components
$ npm init -y

npm init -y will create a package.json file.

"name": "npm-components", // component name
"version": "1.0.0", // component version
"description": "", // component description
"main": "index.js", // component entry file
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"keywords": [],
"author": "", // component author
"license": "ISC" // component license

main: The entry file of the component. default is index.js.

now we can start to create index.js file.

// index.js
const hello = "hello";
const world = "world";
module.exports.log = function () {
console.log(hello + world);

a very simple npm package project is created. This project only contains a index.js file and a package.json file.

npm version specificationโ€‹

The version format is major.minor.patch, and the increment rule is as follows:

major: When you make incompatible API changes, minor: When you make backward-compatible feature additions, patch: When you make a backward-compatible problem fix.

The prior version number and version build information can be appended to major.minor.patch as an extension. Such as: 1.0.0-alpha.0.

Before each release, you need to confirm the updated content, select the version number, and use npm version to type the version number.

npm version [patch|minor|major]

At this point, version in package.json is incremented by 1, and you can also see the version number change in some command-line tools.

~/npm-components  (npm-components@1.0.0)
$ npm version patch


~/npm-components  (npm-components@1.0.1)

Published to the npmโ€‹

Once the development is complete and the version number is marked, publishing to npm is a step-by-step process:

  1. If it is an open source project on the public network, you need to register an account on npm official registry.

  2. If your company has an internal npm registry, you just need to find the npm manager to add it to you.

  3. If other sources are used, switch to the npm official registry. We use the nrm to manage npm registry.

nrm ls

npm --
yarn --
cnpm --
taobao --

nrm use npm
  1. Run the command line tool npm login in the root directory of the project. The system prompts you to enter your personal information.

  2. Run npm publish to publish the project to npm.

  3. If the upload process is successful. you can see your npm package in the npm registry.

Note: npm package can be deleted for 72 hours, after which it can never be deleted, so don't post meaningless packages. If uninstallation is required, perform it within 72 hours of release:

$ npm unpublish npm-components@1.0.1

That's all. thanks for reading.