NPM is a package manager installed with Node.js. Users can download modules written by others from the NPM server to improve development efficiency
You can run the NPM –version command to view the NPM version, and run the NPM install NPM -g command to upgrade the NPM version
1. Install the module
You can use the NPM install command to install modules. There are two ways to install modules: local installation and global installation.
- Locally installed modules are placed in the current directory
node_modules
In the directory, you can passrequire()
The introduction of the program - Globally installed modules are placed in the global directory and can be used directly from the command line
To provide both functions, you need to install modules locally and globally. The common commands and parameters are as follows:
> Install dependencies from the package.json configuration file in the current file
> npm install
> npm i # shorthand
> # Install modules locally
> npm install moduleName
> # Global install module
> npm install moduleName --golbal
> npm install moduleName -g # shorthand
> Add the following dependencies to the package.json file:
> npm install moduleName --save
> npm install moduleName -S # shorthand
> Write devDependencies in package.json.
> npm install moduleName --save-dev
> npm install moduleName -D # shorthand
Copy the code
2. Uninstall the module
You can use the NPM uninstall command to uninstall modules. The common commands and parameters are as follows:
> # Delete specified module (local)
> npm uninstall moduleName
> # delete specified module (global)
> npm uninstall moduleName --global
> npm uninstall moduleName -g # shorthand
> Json < span style = "box-sizing: border-box; line-height: 21px
> npm uninstall moduleName --save
> npm uninstall moduleName -S # shorthand
> Delete devDependencies from package.json
> npm uninstall moduleName --save-dev
> npm uninstall moduleName -D # shorthand
Copy the code
3. Check information
You can use the NPM list command to view the installed modules. The common commands and parameters are as follows:
> # View all installed modules (local)
> npm list
> # View all installed modules (global)
> npm list --global
> npm list -g # shorthand
> # specify that the depth of expansion is zero, that is, no dependent modules are displayed
> npm list --depth 0
> View the specified module
> npm list moduleName
Copy the code
For more detailed information, you can look at the package.json file for each module, which is located in the module directory and describes the module’s properties
Some common attributes are as follows:
-
Name: indicates the package name
-
Version: indicates the package version number
NPM uses the semantic version number in the format of X.Y.Z, which indicates the major version number, minor version number, and patch version number respectively
If the bug is fixed, update Z; If new functions are added but backward compatible, update Y; Update X if there are major changes and they are not backward compatible
-
Description: indicates the description of the package
-
Main: entry file. The default file is the index.js file in the root directory
-
Dependencies: List of dependent packages in the production environment
-
DevDependencies: List of dependent packages in the development environment
-
Script: specifies the script command, which can be run using NPM run
on the command line
-
Author: indicates the author name
-
License: indicates the license type
-
Homepage: the official website address
-
Repository: repository information
4. Update the module
You can use the NPM update command to update modules. The following commands and parameters are commonly used:
> Update all modules (local)
> npm update
> Update all modules (global)
> npm update --global
> npm update -g # shorthand
> Update the specified module
> npm update moduleName
> Update the specified module to the specified version
> npm update [email protected]
> Update the specified module to the latest version
> npm update moduleName@latest
> # Update the execution module while logging the information to package.json file
> npm update moduleName --save
Copy the code
There are two caveats to using the NPM update command. The first is that the version cannot be rolled back, and the second is that only the next version is updated
For example, there are three versions of the LoDash module, 3.9.1, 3.10.1, and 4.17.15
If we now have loDash 3.10.1 installed, it cannot be rolled back to 3.9.1 or updated to 4.17.15
5. Modify the configuration
(0) General format
> npm config --help
# npm config set <key> <value>
# npm config get [<key>]
# npm config delete <key>
# npm config list [--json]
# npm set <key> <value>
# npm get [<key>]
# alias: c
Copy the code
> npm config list
#; cli configs
# metrics-registry = "https://registry.npmjs.org/"
# scope = ""
# user-agent = "NPM /6.4.1 node/v10.15.3 win32 x64" # user-agent =" NPM /6.4.1 node/v10.15.3 win32 x64"
#; userconfig C:\Users\
\.npmrc
# registry = "https://registry.npmjs.org/"
#; builtin config undefined
# prefix = "C:\\Users\\<username>\\AppData\\Roaming\\npm"
#; node bin location = C:\nodejs\node.exe
#; cwd = C:\Users\
\Desktop
#; HOME = C:\Users\
#; "npm config ls -l" to show all defaults.
Copy the code
For NPM, there are seven different levels of configuration, in descending order of priority:
- Command line arguments
- Environment variable: to
npm_config_
The environment variable at the beginning is considered an NPM configuration - project
.npmrc
Configuration files: Located in the project root directory - User configuration: Using commands
npm config get userconfig
You can view the configuration file path - Global configuration: Use commands
npm config get globalconfig
You can view the configuration file path - built-in
.npmrc
Configuration file: located in the installation directory of NPM - The default configuration
(1) Set the proxy
> # check agent, the default for https://registry.npmjs.org/
> npm config get registry
> # modify proxy
> npm config set registry https://registry.npm.taobao.org
Copy the code
(2) Set the default path for the global installation
> \Users\
\AppData\Roaming\ NPM
> npm config get prefix
> Change the default path for the global installation
> npm config set prefix C:\nodejs\npm
Copy the code
(3) Set the cache path
> \Users\
\AppData\Roaming\ NPM -cache
> npm config get cache
> Change the cache path
> npm config set cache C:\nodejs\npm-cache
Copy the code
6, NPX
(1) Introduction
Finally, if we say NPM is a package manager, then NPX is a package executor, what does that mean?
We know that locally installed packages can only be used in projects, and only globally installed packages can be used at the command line, which can cause some inconvenience
NPX is designed to solve this problem by running the specified module directly from the command line
(2) Principle
By default, NPX is first looked up in the project’s node_modules/. Bin directory and the system environment variable $PATH
If not, NPX downloads the module to a temporary directory, uses it, and then deletes it. There are two parameters that change this default behavior
--no-install
: Force the local module to be used instead of downloading the remote module. If the local module is not found, an error is reported--ignore-existing
: Forces the remote module to be used, even if the module is locally available
3. Examples
- Not installed globally
create-react-app
In case of usecreate-react-app
Create a scaffold
npx create-react-app my-app
Copy the code
- Execute script files using a specific version of Node
NPX [email protected] test. JsCopy the code
(4) Think
If we don’t use NPX, is there a way to execute the modules installed in the project from the command line? The answer is yes, it’s just a bit more difficult
The first way is to write a full path to node_modules/.bin/moduleName
The second method is used in the script field of the package.json file