This article will cover NPM briefly, but if you are in a hurry to eat, skip the section “1 NPM”.

1 npm

1.1 introduction of NPM

Anyone interested in reading this article will probably know what NPM is.

Here is an introduction to NPM from the W3C.

NPM is a package management tool installed with NodeJS, which can solve many problems in NodeJS code deployment. The common usage scenarios are as follows:

  • Allows users toNPMThe server downloads third-party packages written by others and uses them locally.
  • Allows users toNPMThe server downloads and installs command line programs written by others for local use.
  • Allows users to upload their own packages or command-line programsNPMThe server is used by others.

That is, to use NPM, you need to install Node.

You can download the node version from the Node official website and install it directly.


1.2 Registering an NPM Account

To create your own NPM toolkit, you first need to register an NPM account.

Registration is available on the NPM website.


1.3 Viewing and Switching NPM Sources

You can use the NRM tool to view and switch NPM sources

NRM installation command

npm install nrm -g
Copy the code

View the NPM source currently in use

nrm ls
Copy the code

The first term is preceded by an asterisk (*) to prove that the first term is currently in use (NPM)

If you need to switch sources, you can use this command (here taobao is an example)

nrm use taobao
Copy the code

If the switch succeeds, the prompt above will appear.

Then use NRM ls to check again, and it can be seen that taobao source has been successfully switched.

But this article is about publishing the toolkit to NPM, so switch back to the first item.




2 Start building scaffolding

2.1 Initializing the Project

First, create the project directory (folder). This article uses xzy-test as an example.

Open the terminal, enter the project, and initialize the project using the following command

npm init -y
Copy the code

After successful initialization, a package.json file will appear in the project root directory.

Create the bin directory in the project root directory, and then create index.js in the bin directory.

This is where we’re going to code, but the coding will be left to the next section, and the main thing here is to clean up the directory structure.

The project is then configured through the package.json file

{
  "name": "xyz-test"."version": "0.0.1"."description": ""."bin": {
    "xyz-test": "bin/index.js"
  },
  "main": "index.js"."scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": []."author": ""."license": "ISC"
}
Copy the code

This command will execute bin/index.js if you use xyz-test.


2.2 coding

Open bin/index.js and type the following.

#! /usr/bin/env node
console.log('Hello ZYX')
Copy the code

The first line #! /usr/bin/env node is mandatory. After scaffolding is installed, Node is automatically found in the global environment to help execute the code.

The second line is the functional code, which can be written depending on the development environment if it is a module package.


2.3 login NPM

Registration is required on the NPM website.

Make sure that the NPM source you are using is pointing to NPM before logging in. You can click 1.3 Viewing and Switching NPM Sources.

Run the following command on the terminal to log in

npm login
Copy the code

Then enter your account, password and email address as prompted.


2.4 release

Open the terminal, enter the root directory of the project, and enter the following command to publish.

publish
Copy the code

After the successful release, you can log on to the NPM website to view the newly released project.


2.5 The version number needs to be changed for iteration

If a project update needs to be republished, the version number in package.json must be changed.

The version number can only go up.

"version": "Hundreds"
Copy the code




3 Using scaffolding

3.1 download

On the NPM web site, open the project you just released and see the installation command prompt on the right.

npm install xyz-test -g
Copy the code

-g indicates global installation


3.2 the use of

Enter the following command on the terminal

xyz-test
Copy the code


3.3 delete

npm uninstall xyz-test -g
# or
npm remove xyz-test -g
Copy the code




4 Websites related to this article

NPM’s official website

The node’s official website