NPM is a Node package management and distribution tool, and has become the unofficial standard for releasing Node modules (packages). With NPM, you can quickly find the packages to use for a specific service, download, install and manage installed packages.
Commonly used commands for NPM are:
(1) $ npm install moduleNames
Install Node module
Note: If you do not know the name of the module when using it, you can follow the http://search.npmjs.org website
Index the value to find the desired module. npm also provides the function of querying $npm search indexName
After installation, a node_modules directory will be generated, and the various node modules installed are in the directory.
The installation of node is divided into global mode and local mode. Generally, 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 it under node_modules in the installation directory of Node. The global installation command is
$ npm install -g moduleName. Know that using $npm set global=true to set the installation mode
, $npm get global can view the currently used installation mode.
(2) $ npm view moduleNames
Check out the package.json folder of the node module
Note: If you want to view the content of a certain tag under the package.json folder, you can use it
$ npm view moduleName labelName
(3) $ npm list
View the installed node packages in the current directory
Note: Node module search starts from the current directory where the code is executed, and the search results depend on the directory currently used.
content under node_modules. $ npm list parseable=true can be displayed in the form of a directory.
All node packages installed before
(4) $ npm help
View Help Commands
(5) $ npm view moudleName dependencies
View the dependencies of the package
(6) $ npm view moduleName repository.url
View the source file address of the package
(7) $ npm view moduleName engines
View the version of Node on which the package depends
(8) $npm help folders
View all folders used by npm
(9) $ npm rebuild moduleName
Used to rebuild after changing the content of the package
(10)$ npm outdated
Check whether the package is outdated. This command will list all outdated packages and can update the package in time.
(11) $ npm update moduleName
Update node module
(12)$ npm uninstall moudleName
Uninstall the node module
(13) An npm package is a folder containing package.json. package.json describes the structure of this folder. visit
The method to ask npm's json folder is as follows:
$ npm help json
This command will open a web page in the default way. If you change the default opening program, it may not be typed as a web page.
open.
(14) When publishing an npm package, you need to check whether a package name already exists
$ npm search packageName
(15) Many times when we use an npm package, we often forget to require its interdependent modules. We can
Use the following command to view what packages this module depends on
npm is the package manager for Node.JS. When developing Node.JS, it is often used to install/uninstall packages. In fact, it is also done by publishing a package.
Configure package.json
To package a program, you must first set up various settings, which are specified by package.json in the root directory of the package. The content of package.json must be in strict JSON format, that is:
Strings should be enclosed in double quotes, not single quotes;
The attribute name must be double quoted;
Don't add a comma after the last attribute.
There are many attributes of configuration objects, please refer to here for details. Here are some commonly used items:
name: The package name cannot be repeated with existing packages.
version: version number.
Description: A brief introduction.
author: Author information. Contains three attributes: name, email, and url.
bin: If there is an executable file in the program (mainly called from the command line), specify it here and you can specify multiple files.
main: The program entry when calling this package using require.
dependencies: dependent package, you can specify the version number.
After configuring package.json, you can package and install it locally to test whether the program is operating normally. The installation command is:
npm install <local path>
In addition, there is another unspoken rule to note that if you want the executable program in the package to run in the Node.JS environment, then please add a line like this in front of the program entry file:
#!/usr/bin/env node
Without this line, it will be turned on in the system default mode, rather than running in the Node.JS environment.
Register an npm account
To publish the package to npm, you also need to register an account first. npm does not provide a web version of the registration wizard. Registration must also be carried out through the command line:
npm adduser
After executing this command, a prompt for entering the user name, email, and password will appear in turn. After entering, you can wait for a while.
Publish packages
After the preparations are done, you can publish the package by executing the following command:
npm publish <local path>
If you want to update the package, just modify the version number in package.json and then execute the release command again.