How to publish an npm application?

preface

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

Bash

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

Build a project

Bash

npm init

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: keywords, convenient for npm package retrieval, 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 files such as README.md

As the saying goes, README.md plays a great role in whether a project is used. 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.js file in the root directory. The following is just the test code, which has no practical significance, but is convenient for everyone to understand.

Js

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

Publish npm package

  1. stay https://www.npmjs.com To register an account, you must remember your account password. I just suffered a loss.
  2. Add npm account locally
npm adduser

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 https://registry.npmjs.org/

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.

Bash

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

Bash

npm install json-tree-diff

report errors

Bash

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 https://www.npmjs.com Find your bag.

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

summary

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.

Keywords: node.js Front-end npm React

Added by kee1108 on Tue, 12 Oct 2021 06:56:18 +0300