node. js NPM Detailed

Source: Internet
Author: User

node. js npm Details One, NPM introduction

Install NPM Please read the predecessor's article, very detailed introduction.

The full name of NPM: Node package Manager.

(1) Popular understanding

In fact, you can literally understand what this product does, translating it into "node package manager." Yes, that's a management tool for node's package, and now I'm trying to have

    1. Download and install the package (NPM install [pkg])
    2. Upgrade installation package (NPM Update [pkg])
    3. Uninstalling the installation package (NPM UNINSTALL/RM [pkg]), you can specify the version number of the uninstall package ...

In fact, these commands are very simple, commonly used must remember, not commonly used queries can, this is a better way to learn knowledge.

In Terminal input:

//查看npm拥有的全部命令$ npm --help$ npm help//查看某一个npm命令的详细用法$ npm <command> --help$ npm help <command>
(2) Professional explanations

NPM (Node Package Manager) is the mainstream Suite manager under node. js. It is built into the node system after the node. js v0.6.x version. NPM helps developers install, uninstall, delete, update the node. JS Suite, and release their plugins via NPM.

Second, similar products

In fact, learning a product, you can contact other products, to better understand the products at hand now. The first time I learned npm my first reaction was, much like the Linux/mac platform of many software, depending on the management of the way can refer to maven ... Of course, similarity can be casually associative.

Next, let's take a few examples, of course, to find out more about Baidu && Google.

    1. Gem
    2. Pypl
    3. Pear
    4. Macport
    5. Homebrew
    6. Rem
    7. Apt-get
    8. Yum ...

is not a lot familiar? This knowledge of NPM does not have to be limited to concepts.

Third, NPM basic functions (1) NPMRC file Introduction

First introduce the NPMRC file, which is the configuration file for the NPM Package Manager.

Three files associated with NPMRC:

    1. User profiles: ~/.NPMRC
    2. Global configuration file: $PREFIX/NPMRC
    3. NPM Internal profile: Under the directory where NPM is installed

Here's a closer look at the configuration of NPM Config.

(2) NPM gets the configuration 6 ways (priority from high to low):

1. Command-line arguments

$ --proxy http://<server>:<port>

2. Environment variables

Environment variables prefixed with "npmconfig" will be considered to be the configuration properties of NPM. Like the concept of Maven mirroring, it's easy to communicate.

$ npm_config_proxy=http://<server>:<port>

3. User Configuration Files

//查看文件路径$ npm config get userconfig//mac系统默认路径$HOME/.npmrc

4. Global configuration file

//查看文件路径$ npm config get globalconfig//mac系统默认路径/usr/local/etc/npmrc

Effect of input terminal in 3,4

5. Built-in configuration file

Install the NPMRC file in the NPM directory.

6. Default configuration

If none of the first 5 is set, NPM uses the default configuration parameters.

(4) NPM Install

"Install the specified package": This command is not difficult, but there is a need to note that there are two types of installation, the following will be explained separately.

If you do not know the specific name of the package, you can search on the http://search.npmjs.org.

(5) NPM Uninstall

"Uninstall the specified package": At the time of help, will recommend the NPM RM this command, uninstall will uninstall the substitution of the dependency, RM.

(6) NPM ls

View the installed package manifest, in fact, and the Linux ls command very much like, can follow a lot of parameters, details can be used

$ npm help ls
(7) NPM Search

Search the package For more information, such as our search for express try. The first search, you will be prompted to set up an index, you need to wait patiently for a moment, you do not switch off the terminal when testing.

npm WARN Building the local index for the first time, please be patient

It looks complicated, but it's a little bit big, but it consists of the following 6 parts:

    1. Name
    2. Describe
    3. Author
    4. Release time
    5. Release version number
    6. Key words
(8) NPM update

Update the installed Packages

More APIs to view official website: https://npmjs.org/doc/

Iv. knowledge of the version number.

In the Package.json configuration file in node. js, we need to configure the version number, such as 0.1.2

First digit: Major version number

Second digit: Sub-version number

Third digit: Patch version number

Find a good introduction to the software project version number of the article

Naming rules and formats for software project version numbers

Why do you have to explain this? Certainly is useful, because NPM installs the time to be possible to choose the version number, a bit understanding will be better, at least I think so.

The configuration of the first half of node and NPM can be referenced in two previous articles:

    1. Hello Node
Iv. Local mode and global mode

If you understand the environment variables inside, user variables and system variables. Can do an analogy to understand. Of course, the concept of environment variables above windows is better understood.

1. Local mode

Features of the installation package in local mode

    • The path variable is not written (that is, the environment variable, it cannot be referenced globally and cannot be used directly at the terminal)
    • Ability to install different versions of the installation package in different Node_modules directories
    • Ability to introduce installation packages via require ()

Packages installed using NPM install [@] are installed by default in the "Node_modules" directory of the current directory (if there is no such directory, it will be created automatically when the command is executed).

//专业的写法./node_modules

(1) Local mode installation by default

npm install <pkg>

(2) Information written to Package.json file

npm install <pkg> --save

This command writes the information to Package.json while the package is being installed.

@version indicates the version number of the specified installation package, which is an optional project and the latest version is installed by default.

If you have a Package.json file in the project path, you can install all the dependent packages based on the dependencies configuration using the NPM install method.

If this is configured, you do not have to commit the Node_modules folder when the code is submitted to GitHub.

2. Global mode

Features of the global mode installation package

    • No duplicate installation required
    • Cannot use require () to introduce
    • Path is written and a soft link is created, using the command line
    • Inconvenient to specify a specific version to run

(1) Install with global mode

npm install -g <pkg>

(3) Global directory in Mac

//安装包所在目录/usr/local/lib/node_modules///运行命令的软链接所在目录/usr/local/bin

(4) View the installation package path

//查看当前包的安装路径npm root//查看全局的包的安装路径npm root -g

(5) Setting the Global mode installation directory

//设置后,以全局模式将会安装在此目录中,不过需要手动加入PATH,切记npm config set prefix <global dir>//设置npm缓存文件的存放路径npm config set cache <cache dir>

(6) View default mode

//默认返回:false$ npm get global$ npm config get global

(7) Set to the default in global mode installation, you do not have to add "-G" parameter each time.

$ npm set global=true$ npm config set global=true

Node. js NPM Detailed

Related Article

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.