My node. js learning path (ii) NPM module management
- 2020-03-30 03:30:12
- OfStack
NPM is a Node package management and distribution tool that has become the unofficial publishing standard for Node modules (packages). With NPM, you can quickly find packages to use for a particular service, download, install, and manage packages that have already been installed.
Common NPM commands There are:
(1) $NPM install moduleNames
Install the Node module
Note: if you don't know the name of the module when you use it, you can follow it through the (link: http://search.npmjs.org) website
The index value finds the desired module. NPM also provides the ability to query $NPM search indexName
After the installation, a node_modules directory is generated, which contains the installed node modules.
Node is installed in both global and local modes. Typically it will run in local mode and the package will be installed
Go to the local node_modules directory with your application code statistics. In global mode, the Node package will be
Install to node_modules in the Node installation directory. The global installation command is
$NPM install -g moduleName. Learn to set the installation mode using $NPM set global=true
, $NPM get global to view the currently used installation mode.
(2) $NPM view moduleNames
View the package.json folder for the node module
Note: if you want to see the contents of a label under the package.json folder, you can use
$NPM view moduleName labelName
(3) $NPM list
View the node packages installed in the current directory
Note: the Node module search starts in the current directory where the code is executed, and the search results depend on the current directory being used
Node_modules. $NPM list parseable=true can be represented as a directory when
All previously installed node packages
(4) $NPM help
View help command
(5) $NPM view moudleName dependencies
View package dependencies
(6) $NPM view moduleName repository. Url
View the package's source file address
(7) $NPM view moduleName engines
View the version of Node that the package depends on
(8) $NPM help folders
View all the folders used by NPM
(9) $NPM rebuild moduleName
Used to rebuild after changing the package content
(10) $NPM outdated
Check if the package is out of date. This command lists all the packages that are out of date so that the package can be updated in a timely manner
(11) $NPM update moduleName
Update node module
(12) $NPM uninstall moudleName
Uninstall node module
(13) an NPM package is a folder containing package.json, which describes the structure of the folder. During his visit to
Ask NPM for the json folder as follows:
$NPM help json
This command opens a web page by default and may not be typed as a web page if you change the default open program
Open it.
(14) when releasing an NPM package, you need to verify that a package name already exists
$NPM search packageName
(15) many times when we are using an NPM package, we often forget the module requiring its interdependence. We can
Use commands such as the following to see which packages the module depends on
NPM is the package manager for node. JS. When developing node. JS, you often use it to install/uninstall packages. In fact, it does the job of distributing the package.
Configuration package. Json
To package the program, you first need to set up the Settings specified by package.json in the root of the program. The contents of package.json must be in strict json format, that is:
Strings should be enclosed in double quotes, not single quotes;
Attribute names must be quoted;
Never add a comma after the last attribute.
There are many properties of the configuration object, you can see here, here is a list of commonly used items:
Name: package name, which cannot be repeated with an existing package.
Version: version number.
Description: a short description.
This is the author information. Contains three attributes: name, email, and url.
Bin: if there are executable files in the program (mainly called from the command line), specify them here. You can specify more than one.
Main: the program entry when the package is called using require.
Dependencies: the version number can be specified.
After configuring package.json, you can first package and install it locally to test whether the program works normally. The installation command is:
NPM install < Local path >
#! The/usr/bin/env node
Without this line, it will open as the system default, rather than running in a node.js environment.
Sign up for an NPM account
To publish the package to NPM, you also need to register an account. NPM does not provide a web version of the registration wizard. Registration is also done from the command line:
NPM adduser
After executing this command, the user name, Email, password will appear in turn, input after waiting for a while.
Release package
With all the preparation in place, execute the following command to publish the package:
NPM publish < Local path >
If you want to update the package, simply change the version number in package.json and re-execute the publish command.