Details the Node. js package project directory and the use of NPM package manager

  • 2020-12-21 17:57:45
  • OfStack

Project directory

With this knowledge in mind, we can now plan a complete project catalog. Take writing a command-line program as an example. Generally we will provide both command-line mode and API mode, and we will write the code with the help of a three-way package. In addition to code, a complete program should also have its own documentation and test cases. Therefore, a standard project directory will look like this.


- /home/user/workspace/node-echo/  #  Project directory 
  - bin/             #  Store the command line related code 
    node-echo
  + doc/             #  documentation 
  - lib/             #  store API The relevant code 
    echo.js
  - node_modules/         #  store 3 The package 
    + argv/
  + tests/            #  Store test cases 
  package.json          #  Metadata file 
  README.md            #  Description file 

Some of the files are as follows:


/* bin/node-echo */
var argv = require('argv'),
  echo = require('../lib/echo');
console.log(echo(argv.join(' ')));

/* lib/echo.js */
module.exports = function (message) {
  return message;
};

/* package.json */
{
  "name": "node-echo",
  "main": "./lib/echo.js"
}

In the example above, the categories hold different types of files and load modules directly using the 3-square-package name through the node_moudles directory. In addition, with package. json defined, the ES14en-ES15en directory can also be used as a package.

NPM

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

Allows users to download 3-party packages written by others from the NPM server for local use. Allows users to download and install command line programs written by others from the NPM server for local use. Allows users to upload packages or command-line programs they have written to the NPM server for others to use.

As you can see, NPM has built an NodeJS ecosphere where NodeJS developers and users can exchange needed items. Here's how to use NPM in each of these scenarios.

Download 3 square packs
When you need to use 3-dimensional packages, you first need to know what packages are available. While npmjs.org offers a search box based on the package name, if you're not sure of the name of the 3-square-package you want to use, check baidu 1. Once you know the package name, such as argv in the above example, you can open the terminal in the project directory and use the following command to download the 3 square packages.


$ npm install argv
...
argv@0.0.2 node_modules\argv

Once downloaded, the argv package is placed in the node_modules directory under the project directory, so you only need to go through require('argv') in your code without specifying a 3-way package path.

The above command downloads the latest version 3 package by default. If you want to download the specified version, you can add @ after the package name < version > For example, argv version 0.0.1 can be downloaded from the following command.


$ npm install argv@0.0.1
...
argv@0.0.1 node_modules\argv

If you use more than 3 square packets, it is too human to install one packet and one command in the terminal. NPM therefore extends the field of ES57en.json to allow the declaration of a 3-pack dependency. Therefore, package. json in the above example can be rewritten as follows:


{
  "name": "node-echo",
  "main": "./lib/echo.js",
  "dependencies": {
    "argv": "0.0.2"
  }
}

After this, you can use the npm install command to batch install 3 packages in the project directory. More importantly, when node-ES67en is also uploaded to the NPM server in the future, when someone downloads the package, NPM will automatically download the step 1 dependent 3 packages based on the 3-party package dependency stated in the package. For example, when using the npm install ES72en-ES73en command, NPM automatically creates the following directory structure.


- project/
  - node_modules/
    - node-echo/
      - node_modules/
        + argv/
      ...
  ...

In this way, users only need to care about the 3-dimensional package they use directly, and do not need to resolve all the package dependencies themselves.

Install the command line program
Downloading and installing a command-line program from the NPM service is similar to the 3-way package. For example, es82EN-ES83en in the above example provides the command line as long as ES84en-ES85en itself has configured the relevant ES86en.json field. The user only needs to use the following command to install the program.


$ npm install node-echo -g

The -ES91en parameter indicates the global installation, so es92EN-ES93en will be installed by default to the following location, and NPM will automatically create the soft chain file required under Linux or the.cmd file required under Windows.


- /usr/local/        # Linux Under the system 
  - lib/node_modules/
    + node-echo/
    ...
  - bin/
    node-echo
    ...
  ...

- %APPDATA%\npm\      # Windows Under the system 
  - node_modules\
    + node-echo\
    ...
  node-echo.cmd
  ...

Post code
You need to register an account before you publish the code the first time using NPM. Run npm adduser under the terminal and then follow the prompts. Once the account is complete, then we need to edit the ES105en.json file and add the required fields for NPM. Following the above example of ES108en-ES109en, the necessary fields in ES110en.json are as follows.


{
  "name": "node-echo",      #  The package name, in NPM Only must be maintained on the server 1
  "version": "1.0.0",      #  Current version number 
  "dependencies": {       # 3 The square package depends on the package name and version number 
    "argv": "0.0.2"
   },
  "main": "./lib/echo.js",    #  Location of entry module 
  "bin" : {
    "node-echo": "./bin/node-echo"   #  Command line program name and main module location 
  }
}

From there, we can run the npm publish release code in the same directory as ES115en.json.

The version number
The version number is touched when you download and distribute code using NPM. NPM uses semantic version numbers to manage code, which is briefly described here.

The semantic version number is divided into X.Y.Z 3 bits, which represent the major version number, the minor version number and the patch version number respectively. When code changes, the version number is updated as follows.

+ If you are just fixing bug, you need to update the Z bit.

+ The Y bit needs to be updated if new features are added but backward compatible.

+ The X bit needs to be updated if there is a big change and it is not compatible downward.
With this guarantee, a version number can depend on a range of version numbers in addition to a fixed version number when declaring a 3-party package dependency. For example, "argv": "0.0.x" means to rely on the latest version of the 0.0.ES142en series, argv. All version number ranges supported by NPM can be specified in the official documentation.

1 point to organize it
NPM provides many functions in addition to those described in this chapter, and there are many other useful fields in ES149en.json. In addition to the official documentation available on ES151en.org /doc/, here are 1 more common NPM commands.

NPM provides many commands, such as install and publish, and using npm help you can see all the commands.

Use npm help to see detailed help for a command, such as npm help install. Using npm install. -ES172en in the package.json directory allows you to install the current command line program locally, which can be used for local testing prior to publication. Using npm update < package > You can update the corresponding module in the node_modules subdirectory under the current directory to the latest version. Using npm update < package > -g can update the corresponding command line program of the global installation to the latest version. Using npm cache clear clears the NPM local cache for people who release new versions of code using the same version number. Using npm unpublish < package > @ < version > You can undo the release of a version of your code that you have released.

Related articles: