Npm is a module dependency management tool for Node. js. As a tool used by developers, it mainly solves problems encountered during Node. js development. Like RubyGems's importance to Ruby developers and Maven for Java developers, npm's importance to Node. js developers and communities is self-evident. NPM is a Node package management and distribution tool and has become an unofficial standard for releasing Node modules (packages. With NPM, you can quickly find the packages to be used by specific services, download, install and manage installed packages.
Common NPM commandsInclude:
(1) $ npm install moduleNames
Install the Node Module
Note: If you do not know its name when using the module, you can follow
Index value to find the desired module. Npm also provides the query function $ npm search indexName
After the installation is complete, a node_modules directory is generated, under which each node module is installed.
Node installation is divided into global mode and local mode. Generally, the package runs in local mode and is installed.
Go to the local node_modules directory for statistics with your application code. In global mode, the Node package is
Install it in node_modules under the Node installation directory. The global installation command is
$ Npm install-g moduleName. We learned to use $ npm set global = true to set the installation mode.
, $ Npm get global can view the current installation mode.
(2) $ npm view moduleNames
View the package. json folder of the node Module
Note: To view the content of a tag in the package. json folder, you can use
$ Npm view moduleName labelName
(3) $ npm list
View the installed node packages in the current directory
Note: The Node module search starts from the current directory of code execution. The search result depends on the directory currently in use.
Node_modules. $ Npm list parseable = true can be displayed as a directory when
All node packages installed before
(4) $ npm help
View help commands
(5) $ npm view moudleName dependencies
View package Dependencies
(6) $ npm view moduleName repository. url
View the package source file address
(7) $ npm view moduleName engines
View the Node version on which the package depends
(8) $ npm help folders
View All Folders used by npm
(9) $ npm rebuild moduleName
Used to change the package content and recreate it.
(10) $ npm outdated
Check whether the package is out of date. This command will list all outdated packages and update the packages in time.
(11) $ npm update moduleName
Update node Module
(12) $ npm uninstall moudleName
Uninstall node Module
(13) an npm package contains a package. json folder. package. json describes the structure of this folder. Access
The npm json folder is as follows:
$ Npm help json
This command opens a webpage by default.
(14) When releasing an npm package, check whether a package name already exists.
$ Npm search packageName
(15) when we use an npm package, we often forget the modules on which require depends. We can
Use the following command to view the packages that the module depends on.
Npm is the package manager of Node. JS. During Node. JS development, it is often used to install/uninstall the package. In fact, the work of releasing a package is also done by it.
Configure package. json
To package a program, you must configure various settings, which are specified by package. json in the root directory of the program package. The content of package. json must be in strict JSON format, that is:
A string must be enclosed in double quotation marks rather than single quotation marks;
Attribute names must be enclosed in double quotation marks;
Do not add a comma after the last attribute.
There are many attributes of the configuration object. For details, refer to the following common items:
Name: the package name, which cannot be the same as the existing package.
Version: version number.
Description: a brief introduction.
Author: the author's information. Includes three attributes: name, email, and url.
Bin: if the program contains executable files (called mainly in the command line), you can specify multiple files here.
Main: The program entry for calling this package using require.
Dependencies: the dependent package. You can specify the version number.
After package. json is configured, You can package and install package locally to test whether the program works properly. The installation command is:
Npm install <本地路径>
In addition, you should note that if you want the executable programs in the package to run in the Node. JS environment, add the following line at the beginning of the program entry file:
#! /Usr/bin/env node
Without this line, it will be opened by default, instead of running in the Node. JS environment.
Register an npm account
To publish the package to npm, you must register an account first. Npm does not provide a webpage registration wizard. The registration should also be done through the command line:
After the command is executed, a prompt is displayed, indicating the user name, Email, and password. After the command is entered, wait for a while.
After the preparation is complete, you can run the following command to release the package:
Npm publish <本地路径>
To update the package, you only need to modify the version number in package. json and re-execute the release command.