Introduction to NPM Usage

Source: Internet
Author: User
Tags install node npm install node

NPM is a package management tool that is installed with Nodejs to address many of the issues in Nodejs code deployment, with the following 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 their own packages or command-line programs to the NPM server for others to use.

Since NPM has been integrated with the new version of Nodejs, NPM has been installed as well. You can also use the cmd command line to enter "Npm-v" to test for successful installation. For example, a version prompt indicates that the installation was successful:

NPM applications

NPM has built a nodejs ecosystem where developers and users can exchange their nodejs. Here are three scenarios for NPM applications:

Download third Party Packages

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

$ npm Install argv ... [Email protected] 0.0.2 NODE_MODULES\ARGV

Once downloaded, the argv package is placed in the Node_modules directory under the project directory, so it is only necessary to pass require (' argv ') in the code, without specifying a third-party package path.

The above command downloads the latest version of the third-party package by default, if you want to download the specified version, you can add @<version> after the package name, for example, you can download the 0.0.1 version of argv with the following command.

$ NPM Install [email protected]... [Email protected] 0.0.1 NODE_MODULES\ARGV

NPM extends the fields of Package.json to allow for the declaration of third-party package dependencies. Therefore, the Package.json in the above example can be rewritten as follows:

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

Once this is done, a third-party package can be installed in bulk using the NPM Install command in the project catalog.

More importantly, when Node-echo is also uploaded to the NPM server, when someone downloads the package, NPM automatically downloads the third-party package that depends on the third-party 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 care about the third-party packages they use directly, and do not need to solve all the package dependencies themselves.

Install the command-line program

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

For example, the Node-echo in the previous example provides command-line usage, so long as the Node-echo itself is configured with the relevant Package.json field, only the following command is required for the user to install the program.

$ NPM Install Node-echo-g

The-G in the parameter is the global installation, so Node-echo is installed to the following location by default, and NPM automatically creates the soft-chain files required under the Linux system or the. cmd file that is required under the Windows system.

-/usr/local/               # Linux system     -lib/node_modules/        +    node-echo/... -bin/        node-echo        ...    ... -%appdata%\npm\            # Windows system     - node_modules        + node-echo        ...    Node-echo.cmd     ...
Publish Code

You need to register an account before you use NPM to publish your code for the first time. Run NPM AddUser under Terminal, then follow the prompts.

After the account registration is complete, we need to edit the Package.json file and add the required fields to NPM. Next to the above Node-echo example, the necessary fields in Package.json are as follows.

{    "name": "Node-echo",           # package name, need to maintain    unique "version" on NPM server : "1.0.0",            # current version number    "Dependencies": {              # Third-party package dependent, you need to specify the package        name and version number "argv": "0.0.2"      },    "main": ". Lib/echo.js ",       # ingress module location     " Bin " : {        " Node-echo ":"./bin/node-echo "      # command-line program name and Main module location    }}

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

Version number

When you use NPM to download and publish your code, you will be exposed to the version number. NPM uses the semantic version number to manage the code, which is briefly described here.

The semantic version number is divided into x.y.z three-bit, which represents the major, minor, and patch version numbers, respectively. When the code changes, the version number is updated according to the following guidelines.

    • If you just fix the bug, you need to update the z-bit.
    • If the feature is new, but backwards compatible, you need to update the Y-bit.
    • If there is a large change, the downward is incompatible and the X-bit needs to be updated.

Version number with this guarantee, when declaring a third-party package dependency, in addition to relying on a fixed version of the extra, you can also rely on a range of version numbers. For example, "argv": "0.0.x" is dependent on the latest version of argv for the 0.0.x series.

All version number ranges specified by NPM can be viewed in the official documentation.

NPM Common Commands

In addition to the sections described in this chapter, NPM offers a number of features, as well as many other useful fields in Package.json.

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

NPM offers a number of commands, such as install and publish, and use NPM Help to view all commands.

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

    • Use npm help <command> the detailed help that can be used to view a command, for example npm help install .

    • package.jsonuse in your directory to npm install . -g install the current command-line program locally, which can be used for local testing before publishing.

    • npm update <package>You can node_modules update the corresponding module in the subdirectory of the current directory to the latest version.

    • Use the npm update <package> -g corresponding command-line program to update the global installation to the latest version.

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

    • Use npm unpublish <package>@<version> to revoke a release code that you have published yourself.

Introduction to NPM Usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.