How to publish an npm application- Know


We need to install all kinds of npm every day. How do we publish npm to the remote warehouse? Understanding the process of npm from coding to push ing to npm warehouse will help us debug npm packages better. Next, take a JSON tree diff package I wrote as an example to show the process from writing to publishing an npm package.

Initialize a project

mkdir json-tree-diff
cd json-tree-diff

Build a project

Initialize your npm package

  • Name: the name of the project. Make sure it is unique in the npm package.

  • Version: version number.

  • Description: package description.

  • entry point: the main entry file of the package.

  • Test command: test command. Generally npm run test. Make sure your package has test cases before going online.

  • git repository: git remote address. Fill in any one and modify it after uploading the project later.

  • Keywords: keyword, which is convenient for npm package retrieval. It is a bit like keywords in html.

  • author: This is not explained.

  • License: I don't know the difference between MIT or ISC. Anyway, MIT is right.

The last one is OK? OK, just press enter.

Add open source protocol

The MIT License (MIT)

Copyright (c) <year> <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

Change year into time and copyright holders into your name

Add readme MD and other files

As the saying goes, whether a project is used or not, readme md plays a big role. Therefore, you can imitate the md of well-known projects, such as create React app, React, etc.

Add some other auxiliary files, such as gitignore, if you reference other npm packages, you also need to include package lock json´╝îyarn.lock, eslint and other related files.

Write code

Create a new index in the root directory JS file, the following is just the test code, which has no practical significance, but is convenient for everyone to understand.

module.exports = function(oldJson, newJson) {
  if (oldJson === newJson) {
    return true;
  } else {
    return false;

  1. stay To register an account, you must remember your account password. I just suffered a loss.

  2. Add npm account locally

Fill in the corresponding username, password and email.

There is a hole here. If you installed npm with the image of Taobao, you can't log in successfully. Therefore, the installation address of npm itself needs to be adjusted:

npm config set registry

In the process of logging in, I also encountered this error (the following code). I don't know whether my English is bad or the error prompt is unfriendly. I always thought there was a problem with the configuration. As a result, I checked a lot of information on the Internet and found that my account password may be wrong. Error message when enabling 2-factor authentication for NPM stackoverflow is still awesome.

npm ERR! code EAUTHIP
npm ERR! Unable to authenticate, need: Basic
  1. Publish application
npm publish --access=public

You can publish successfully through the above command, provided that your package name is unique.

  1. Is the test package available

Do not install the package just uploaded in the current directory

npm install json-tree-diff

report errors

npm ERR! code ENOSELF
npm ERR! Refusing to install package with name "json-tree-diff" under a package
npm ERR! also called "json-tree-diff". Did you name your project the same
npm ERR! as the dependency you're installing?

Test with other projects, and you pass Find your bag.

The following figure shows the query results on the official website of npm after the package is sent to npm.


npm provides a public repository for all individual developers' toolkits. We must confirm that our packages are available, easy to read and secure before publishing them. npm has just had a security vulnerability some time ago. Everyone has the responsibility to maintain the public environment.

Writing time: 20180819

Tags: Front-end node.js npm

Posted by sbroad on Thu, 20 Jan 2022 13:39:10 +1030