The installation of the package

Because the NPM’s official Registry server is located abroad, the download may be slow or fail due to network speed. Therefore, after NPM is installed, you need to reset the address of Registry to a domestic address. At present, Taobaoregistry.npm.taobao.orgThe domestic Registry address is provided and is set to that address first. Setting mode:npm config set registry https://registry.npm.taobao.org. After the Settings are complete, run the following commandnpm config get registrycheck

NPM installs a package in two ways:

  1. The local installation
  2. Global installation

The local installation

To complete the local installation, run the NPM install package name or NPM I package name command

Locally installed packages appear in the node_modules directory under the current directory

As development progresses, the node_modules directory becomes too large to transfer directly to production, so the.gitignore file is usually used to ignore the contents of this directory. Local installation works for most packages, It works in the current directory and its subdirectories usually in the root directory of your project using local installation when you install a package, NPM automatically manages dependencies, it downloads that package’s dependencies to node_modules and if the locally installed package has CLI, NPM places its CLI script file in node_modules/. Bin, which can be invoked using the NPX command name

Global installation

Global installation packages are placed in a special global directory, which can be viewed using the command NPM config get prefix

Use the command NPM install –global package name or NPM i-g package name

Important: The global installation package is not available to all projects, it only provides global CLI tools

In most cases, a global installation package is not required unless:

  1. The version of the package is very stable, with few major updates
  2. The CLI tools are frequently used in various projects
  3. The CLI tool supports only the development environment, not the deployment environment

Package configuration

Questions we usually encounter:

  1. How to restore after copy project?
  2. How do you distinguish between development dependency and production dependency?
  3. If your own project is also a package, how do you describe the package information

All of these issues need to be addressed through the package configuration file

The configuration file

NPM treats each project that uses NPM as a package itself, and the package information needs to be described by a configuration file with a fixed name

The name of the configuration file is package.json

This file can be created manually or, more often, through the command NPM init

A lot of information can be described in a configuration file, including:

  • Name: indicates the package name. The name must be an English word character and supports a hyphen
  • Version: version
    • Version specification: Major version number. Minor version. Patch version
    • Major version number: increases only when significant changes have been made to the program, such as important new features, a large number of new apis, or major changes to the technical architecture
    • Minor version number: increases only when minor changes are made to the application, such as adding some minor features or auxiliary apis
    • Patch version: Update only when some bugs are resolved or partial optimizations are made, such as fixing a bug or improving the efficiency of a function
  • Description: indicates the description of the package
  • Homepage: the official website address
  • Author: The author of the package, which must be a valid NPM account name and whose writing specification isaccount <mail>, such as:zhangsan <[email protected]>Incorrect account and email address may cause package publishing failure
  • Repository: The repository address of a package, usually git or SVN, which is an object
    • Type: storage type, git or SVN
    • Address of the url:
  • Main: Package entry file from which package users import package contents by default
  • Keywords: search keyword. After the package is published, the package can be searched by the keyword in the array

Use NPM init –yes or NPM init -y to automatically populate the default configuration when the configuration file is generated

Save dependencies

Most of the time, we just develop the project and don’t package it for distribution, but we still need package.json files

The most important function of package.json files is to record the dependencies of the current project

  • Dependencies: Dependency package of the production environment
  • DevDependencies: Dependencies of the development environment only

After configuring the dependencies, use the following command to install them

## install dependencies dependencies + devDependencies
npm install
npm i

#Install only the production environment dependencies
npm install --production
Copy the code

This way, the code migration is not an issue, just the source code and package.json files, not the node_modules directory, and then the installation can be restored with commands after the migration

To make it easier to add dependencies, NPM supports adding additional parameters when using the install command to save installed dependencies to a package.json file

The commands involved are as follows

#Install dependency to production environmentNPM I Package name NPM I -- Save package name NPM i-s package name
#Install dependencies to the development environmentNPM I --save-dev package name NPM I -d package nameCopy the code

Auto-saved versions of dependencies, such as ^15.1.3, are written as semver versions, which will be explained later

The use of the package

Nodejs supports NPM very well

When importing a module using nodeJS, if the module path does not start with./ or.. /, node thinks the imported module is from the node_modules directory, for example:

var _ = require("lodash");
Copy the code

It first looks for files from the following location in the current directory

Node_modules /lodash. Js node_modules/lodash/ entry fileCopy the code

If no such file exists in the current directory, the system searches for it in the same way

If the file cannot be found in the top-level directory, an error is thrown

The entry files mentioned above are determined according to the following rules

  1. View the package.json file of the imported package and read the main field as the entry file
  2. If the main field is not included, index.js is used as the entry file

The import file rule also applies to modules in their own projects. In Node, you can manually specify a path to import files, which is rare