Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Node .js introduction to NPM usage


May 10, 2021 Node.js


Table of contents


Introduction to NPM usage

This article describes the .js NPM in Node's software, so let's start by understanding what NPM is.

NPM is a package management tool installed with NodeJS that solves many of the problems with NodeJS code deployment, and there are several common usage scenarios:

  • Allows users to download third-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 write to NPM servers for others to use.

Since the new version of nodejs has been integrated with npm, the previous npm has been installed together. Y ou can also enter it

"npm -v"

to test for a successful installation. T he command is as follows, and a version prompt appears to indicate a successful installation:

$ npm -v
2.3.0

If you install an older version of npm, you can easily upgrade with the npm command, which reads:

$ sudo npm install npm -g
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
npm@2.14.2 /usr/local/lib/node_modules/npm

If your Window system uses the following commands:

npm install npm -g

Global installation and local installation

npm package installation is divided into local installation (local), global installation (global) two, from the knock command line, the difference is just whether there is -g, for example

npm install express          # 本地安装
npm install express -g   # 全局安装

If the following error occurs:

npm err! Error: connect ECONNREFUSED 127.0.0.1:8087 

The solution is:

$ npm config set proxy null

Install locally

  • 1. Place the installation package under ./node_modules (the directory in which the npm command was run) and if there is no node_modules directory, the node_modules directory is generated under the node_modules command.
  • 2. Locally installed packages can be introduced through require().

Global installation

  • 1. Place the installation package under /usr/local or your node's installation directory.
  • 2. Can be used directly on the command line.

If you want to have both, you'll need to install it in two places or use npm link.

NPM app

NPM has created a NodeJS ecosystem where NodeJS developers and users can exchange information. Here are three scenarios for NPM applications:

Download the third-party package

We can use the following commands to download third-party packages.

$ npm install argv
...
[email protected] node_modules\argv

Once downloaded, the argv package is placed in the node_modules directory in the engineering directory, so you only need to do it in the code by require ('argv') without specifying a third-party package path.

The above commands download the latest version of the third-party package by default, and if you want to download the specified version, you can add the arrgv version of 0.0.1 at the back of the package name, for example, by using the following command.

$ npm install [email protected]
...
[email protected] node_modules\argv

NPM extends the field of package.json to allow third-party package dependencies to be stated in it. T herefore, the stack.json in the example above can be overwrote as follows:

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

This allows third-party packages to be installed in bulk using the npm install command in the engineering directory.

What's more, when node-echo is uploaded to the NPM server later, and someone downloads the package, NPM relies on the automatic download of the third-party package that is further dependent on the package, as stated in the package.

For example, when using the npm install node-echo command, NPM automatically creates the following directory structure.

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

As a result, users only need to care about the third-party packages they use directly, and they don't need to solve the dependencies of all packages themselves.

Install the command line program

Downloading and installing a command-line program from the NPM service is similar to a third-party package.

For example, the node-echo in the example above provides a command-line usage, as long as node-echo itself is configured with the relevant package.json field, for the user, only the following command installer is required.

$ npm install node-echo -g

-g in the parameter represents a global installation, so node-echo is installed by default to the following locations, and NPM automatically creates the soft-chain files that are required under Linux or .cmd files that are required under Windows.

- /usr/local/               # Linux系统下
    - lib/node_modules/
        + node-echo/
        ...
    - bin/
        node-echo
        ...
    ...

- %APPDATA%\npm\            # Windows系统下
    - node_modules\
        + node-echo\
        ...
    node-echo.cmd
    ...

Publish the code

You need to register an account before you can publish your code using NPM for the first time. Run npm adduser under the terminal and follow the prompts.

After the account registration is complete, we then need to edit the package.json file and add the necessary fields for NPM. N ext to the example of node-echo above, the necessary fields in package.json are as follows.

{
    "name": "node-echo",           # 包名,在NPM服务器上须要保持唯一
    "version": "1.0.0",            # 当前版本号
    "dependencies": {              # 第三方包依赖,需要指定包名和版本号
        "argv": "0.0.2"
      },
    "main": "./lib/echo.js",       # 入口模块位置
    "bin" : {
        "node-echo": "./bin/node-echo"      # 命令行程序名和主模块位置
    }
}

After that, we can run the npm publish release code in the directory where package.json is located.


Version number

Version numbers are accessible when downloading and publishing code using NPM. N PM uses semantic version numbers to manage code, which is briefly described here.

The semantic version number is divided into X.Y.Z, which represents the major version number, the secondary version number, and the patch version number, respectively. W hen the code changes, the version number is updated according to the following principles.

  • If you're just fixing the bug, you need to update the Z-bit.
  • If it's new, but it's down-compatible, you need to update the Y bit.
  • If there is a big change, down is not compatible, you need to update the X bit.

With this guarantee, a third-party package dependency can depend on a range of version numbers in addition to a fixed version number. For example, "argv": "0.0.x" means that it depends on the latest version of the 0.0.x series.

All version number ranges supported by NPM can be specified in such a way that the official documentation can be viewed.


NPM commonly used commands

In addition to the sections covered in this chapter, NPM provides many features, and there are many other useful fields in package.json.

In addition to npmjs.org/doc/ documents in the documentation, here are some common NPM commands.

NPM provides a number of commands, such as install and publis, and uses npm help to view all commands.

  • NPM provides a number of commands, install and publish npm help to view all commands.

  • Use npm help <command> for a command, such as npm help install

  • Use npm install . -g to install the current command line program locally first and can be used for local testing before publication. package.json

  • You can update the corresponding modules in the node_modules subdirectdirectle under the node_modules to the latest version using npm update and npm update <package>

  • You npm update <package> -g latest version using npm update .

  • Use npm cache clear empty the NPM local cache against people who publish new version code with the same version number.

  • You can undo the release of a version of your own version of the code using npm unpublish <package>@<version> .