NPM Module Manager

Source: Internet
Author: User

I. Introduction of NPM

NPM has two levels of meaning. One layer of meaning is the Open module registration and management System of node. JS, which is http://npmjs.org. Another layer of meaning is node. JS's default module manager, which is a command-line software that installs and manages node modules.
NPM does not need to be installed separately. When you install node, NPM is installed together. However, the NPM that came with node may not be the latest version, and it is best to update to the latest version with the following command.
NPM uses the "Semantic version" management package. The so-called semantic version refers to the version number of the form A.B.C, where a is the large version number, B is the minor version number, and C is the patch number.

# 将npm更新到最新版本npmnpm@latestnpmnpm@0.1.1 -g
# 查看npm的版本和配置$ npm -v
# npm命令列表help
# 各个命令的简单用法-l
# 配置信息-l
Second, NPM info
# 查看每个模块的具体信息$ npm info underscore$ npm info underscore description$ npm info underscore homepage$ npm info underscore version
Third, NPM search
# npm仓库搜索某个模块$ npm search <搜索词>
Iv. NPM List
# 列出当前目录安装的所有模块$ npm list
# 列出全局安装的所有模块$ npm -globallist
# npm list命令也可以列出单个模块。$ npm list underscore
V. NPM INSTALL1. Modules can be "installed globally" or "installed locally".
    • "Global Installation" refers to downloading a module directly to the installation directory of node, which can be called by each item.
    • "Local Installation" refers to the download of a module to the Node_modules subdirectory of the current directory, which can then be called only in the current directory and its subdirectories.
# “本地安装”某个模块$ npm install <package name>
# npm也支持直接输入github地址$ npm install git://github.com/package/path.git$ npm install git://github.com/package/path.git#0.1.0
# 使用global参数,可以“全局安装”某个模块npm install -global [package name]
2. The install command can specify which property of the installed module is dependent, that is, in which item of the Packages.json file it appears, the Package.json can be generated automatically via NPM init.
    • –save: The module name is added to the dependencies and can be simplified to the parameter-S.
    • –save-dev: The module name is added to the devdependencies and can be simplified to the parameter-D.

NPM Install installs all the modules in the Dependencies field and the Devdependencies field by default, and if you use the production parameter, you can install only the modules of the Dependencies field.

$ npm install --production# 或者$ NODE_ENV=production npm install
Six, npm update,npm Uninstall
# 升级本地安装的模块npm update -global [package name]
# 删除本地安装的模块npm uninstall [package name] -global
Seven, NPM run

NPM is not available for module management and can also be used to execute scripts. The Package.json file has a scripts field that can be used to specify script commands for NPM to invoke directly.

"scripts": {    "lint""jshint **.js",    "test""mocha test/"  }
# 列出scripts属性下的所有命令npm run
# 执行lint脚本npm run lint

NPM run creates a shell, executes the specified command, and joins the Node_modules/.bin to the path variable, which means that the local module can run directly. That is, NPM run lint directly runs Jshint. JS instead of./node_modules/.bin/jshint. js.
Note : NPM has a built-in two command shorthand, NPM test is equivalent to executing NPM run TEST,NPM start equivalent to executing NPM run start.

Viii. Default Script

When NPM executes certain commands, it executes some default scripts (provided that the scripts are already set up).

    • Prepublish: Executes before releasing a module.
    • Publish, Postpublish: Executes after a module is released.
    • Preinstall: Executes before installing a module.
    • Install, Postinstall: Executes after installing a module.
    • Preuninstall, uninstall: Performs before unloading a module.
    • Postuninstall: Executes after uninstalling a module.
    • Preversion, version: Executes before changing the module version.
    • Postversion: Executes after changing the module version.
    • Pretest, Test, Posttest: Executes when the NPM Test command is run.
    • Prestop, Stop, Poststop: Executes when the NPM Stop command is run.

Prestart, start, Poststart: Executes when the NPM Start command is run.
Prerestart, restart, Postrestart: executes when the NPM restart command is run. If the restart script is not set, then the stop and start scripts are executed sequentially.
NPM Run provides a pre and post two hooks for each command. In the case of NPM run lint, before executing this command, NPM will first see if there are two hooks defined for prelint and postlint, and if so, execute NPM run Prelint first, then run NPM run Lint and execute NPM run Postlint.
If an error occurs, the following script is not executed, that is, if the Prelint script performs an error, then the lint and postlint scripts are not executed.
Also, you cannot add a pre before the pre script, that is, the Preprelint script does not work.

Nine, internal variables

The scripts field can use some internal variables, mainly the various fields of the Package.json.
For example, the content of Package.json is {"name": "foo", "Version": "1.2.5"}, then the value of the variable npm_package_name is foo, and the value of the variable npm_package_version is 1.2.5.

{  "scripts":{    "bundle": "mkdir -p build/$npm_package_version/"  }}
Ten, NPM Publish

(1) Setting up personal information

set init.author.name"ligang"set init.author.email"[email protected]"set init.author.url"https://github.com/381510688/my-git"

(2) requesting a username from the NPM system

$ npm adduser

(3) Login

$ npm login

(4) < not the first time > to release a new version, update the version of the software

npm"<message>"npm1.1.1 ->1.1.2)npm1.1.1 ->1.2.0)npm1.1.1 ->2.0.0)。

(5) Release

$ npm publish

[Reprint please indicate source: http://blog.csdn.net/ligang2585116]

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Reprint please indicate source: http://blog.csdn.net/ligang2585116!

NPM Module Manager

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.