Home  >  Article  >  Web Front-end  >  My Node.js learning journey NPM module management

My Node.js learning journey NPM module management

高洛峰
高洛峰Original
2016-12-26 09:49:201045browse

NPM is a Node package management and distribution tool that has become an unofficial standard for publishing Node modules (packages). With NPM, you can quickly find the packages used by specific services, download, install and manage installed packages.

Commonly used commands for NPM are:

                                                                                        ’ s ’ s ’       through ‐   ‐ ‐ ‐ ​ ​ ​ ​ ​ ​ ​ ​ ‐ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ Str inInstallInstallation ​ ​ ​ ​ ​ ​ ​ ​的 The index value finds the module you want. npm also provides the query function $ npm search indexName
                                                                                                                                                                                                                                                                         
The installation of node is divided into global mode and local mode. Under normal circumstances, it will be run in local mode, and the package will be installed into the local node_modules directory that contains your application code statistics. In global mode, the Node package will be installed into node_modules in the Node installation directory. The global installation command is
                                                                                                                                                       Learned to use $npm set global=true to set the installation mode
                                                                                                              ​


(2) $ npm view moduleNames
View the package.json folder of the node module
Notes: If you want to view the content of a tag under the package.json folder, you can use                             $ npm view moduleName labelName

       (3) $ npm list                                                                                                                                                                                    ​The content under node_modules. $ npm list parseable=true can display all node packages currently installed in the form of a directory


 (4) $ npm help
                 (5)$ npm view moudleName dependencies

                                                                                                                                                                               View package dependencies



(6) $ npm view moduleName repository.url
View the source file address of the package

(7) $ npm view moduleName engines

View the version of Node that the package depends on
​​​

Check whether the package is out of date, This command will list all outdated packages, which can be updated in time

(11) $ NPM Update Moduleename

Update Node module

(12) $ NPM Uninstall MoudLename
Uninstall Node module (13 ) An npm package is a folder that contains package.json, which describes the structure of this folder. The method to access the npm json folder is as follows:
The web page form opens.


(14) When publishing an npm package, you need to check whether a package name already exists
For dependent modules, we can use the following command to check the interdependent packages of this module. npm is the package manager of Node.JS. When doing Node.JS development, it is often used to install/uninstall packages. In fact, it does the work of publishing packages.

Configure package.json

To package a program, you must first configure various settings. These settings 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 to say:

The string must be enclosed in double quotes, not single quotes;
The attribute name must be enclosed in double quotes;
Never after the last attribute Add an extra comma.
There are many attributes of the configuration object. For details, please refer to here. Here are the commonly used items:

name: package name, which cannot be repeated with an existing package.

version: version number.

description: a short 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. You can specify multiple ones.

main: The program entry when calling this package using require.

dependencies: Dependent packages, you can specify the version number.


After configuring package.json, you can package and install it locally first to test whether the program operates normally. The installation command is:

npm install

In addition, there is an unspoken rule to pay attention to , if you want the executable program in the package to run in the Node.JS environment, then please add this line at the front of the program entry file:


#!/usr/bin/env node

if Without this line, it will open as the system default instead of running in the context of Node.JS.


Register an npm account

To publish the package to npm, you need to register an account first. npm does not provide a web version of the registration wizard. Registration also needs to be done through the command line:

npm adduser
After executing this command, you will be prompted to enter your username, email, and password. After entering it, just wait for a while.

Publish the package

The preparations are done. Execute the following command to publish the package:


npm publish

If you want to update the package, just modify the version in package.json number, and then re-execute the release command.

For more articles about my Node.js learning path and NPM module management, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn