📅  最后修改于: 2020-11-03 10:04:46             🧑  作者: Mango
节点包管理器(NPM)提供两个主要功能-
可在search.nodejs.org上搜索的node.js软件包/模块的在线存储库
命令行实用程序,用于安装Node.js软件包,执行版本管理和Node.js软件包的依赖关系管理。
v0.6.3版本之后,NPM与Node.js可安装程序捆绑在一起。要验证相同,请打开控制台并键入以下命令,然后查看结果-
$ npm --version
2.7.1
如果您正在运行旧版本的NPM,则很容易将其更新为最新版本。只需从root使用以下命令-
$ sudo npm install npm -g
/usr/bin/npm -> /usr/lib/node_modules/npm/bin/npm-cli.js
npm@2.7.1 /usr/lib/node_modules/npm
有一个简单的语法可以安装任何Node.js模块-
$ npm install
例如,以下是安装著名的Node.js Web框架模块的命令,该模块名为express-
$ npm install express
现在您可以在js文件中使用此模块,如下所示:
var express = require('express');
默认情况下,NPM在本地模式下安装所有依赖项。这里,本地模式是指位于Node应用程序所在文件夹中的node_modules目录中的软件包安装。可通过require()方法访问本地部署的程序包。例如,当我们安装Express模块时,它在安装Express模块的当前目录中创建了node_modules目录。
$ ls -l
total 0
drwxr-xr-x 3 root root 20 Mar 17 02:23 node_modules
或者,您可以使用npm ls命令列出所有本地安装的模块。
全局安装的软件包/依赖关系存储在系统目录中。这种依赖关系可以在任何node.js的CLI(命令行界面)函数,但不能直接在Node应用程序中使用require()导入。现在,让我们尝试使用全局安装来安装Express模块。
$ npm install express -g
这将产生类似的结果,但是模块将被全局安装。在这里,第一行显示模块版本和安装位置。
express@4.12.2 /usr/lib/node_modules/express
├── merge-descriptors@1.0.0
├── utils-merge@1.0.0
├── cookie-signature@1.0.6
├── methods@1.1.1
├── fresh@0.2.4
├── cookie@0.1.2
├── escape-html@1.0.1
├── range-parser@1.0.2
├── content-type@1.0.1
├── finalhandler@0.3.3
├── vary@1.0.0
├── parseurl@1.3.0
├── content-disposition@0.5.0
├── path-to-regexp@0.1.3
├── depd@1.0.0
├── qs@2.3.3
├── on-finished@2.2.0 (ee-first@1.1.0)
├── etag@1.5.1 (crc@3.2.1)
├── debug@2.1.3 (ms@0.7.0)
├── proxy-addr@1.0.7 (forwarded@0.1.0, ipaddr.js@0.1.9)
├── send@0.12.1 (destroy@1.0.3, ms@0.7.0, mime@1.3.4)
├── serve-static@1.9.2 (send@0.12.2)
├── accepts@1.2.5 (negotiator@0.5.1, mime-types@2.0.10)
└── type-is@1.6.1 (media-typer@0.3.0, mime-types@2.0.10)
您可以使用以下命令来检查全局安装的所有模块-
$ npm ls -g
package.json存在于任何Node应用程序/模块的根目录中,用于定义包的属性。让我们打开存在于node_modules / express /中的Express包的package.json
{
"name": "express",
"description": "Fast, unopinionated, minimalist web framework",
"version": "4.11.2",
"author": {
"name": "TJ Holowaychuk",
"email": "tj@vision-media.ca"
},
"contributors": [{
"name": "Aaron Heckmann",
"email": "aaron.heckmann+github@gmail.com"
},
{
"name": "Ciaran Jessup",
"email": "ciaranj@gmail.com"
},
{
"name": "Douglas Christopher Wilson",
"email": "doug@somethingdoug.com"
},
{
"name": "Guillermo Rauch",
"email": "rauchg@gmail.com"
},
{
"name": "Jonathan Ong",
"email": "me@jongleberry.com"
},
{
"name": "Roman Shtylman",
"email": "shtylman+expressjs@gmail.com"
},
{
"name": "Young Jae Sim",
"email": "hanul@hanul.me"
} ],
"license": "MIT", "repository": {
"type": "git",
"url": "https://github.com/strongloop/express"
},
"homepage": "https://expressjs.com/", "keywords": [
"express",
"framework",
"sinatra",
"web",
"rest",
"restful",
"router",
"app",
"api"
],
"dependencies": {
"accepts": "~1.2.3",
"content-disposition": "0.5.0",
"cookie-signature": "1.0.5",
"debug": "~2.1.1",
"depd": "~1.0.0",
"escape-html": "1.0.1",
"etag": "~1.5.1",
"finalhandler": "0.3.3",
"fresh": "0.2.4",
"media-typer": "0.3.0",
"methods": "~1.1.1",
"on-finished": "~2.2.0",
"parseurl": "~1.3.0",
"path-to-regexp": "0.1.3",
"proxy-addr": "~1.0.6",
"qs": "2.3.3",
"range-parser": "~1.0.2",
"send": "0.11.1",
"serve-static": "~1.8.1",
"type-is": "~1.5.6",
"vary": "~1.0.0",
"cookie": "0.1.2",
"merge-descriptors": "0.0.2",
"utils-merge": "1.0.0"
},
"devDependencies": {
"after": "0.8.1",
"ejs": "2.1.4",
"istanbul": "0.3.5",
"marked": "0.3.3",
"mocha": "~2.1.0",
"should": "~4.6.2",
"supertest": "~0.15.0",
"hjs": "~0.0.6",
"body-parser": "~1.11.0",
"connect-redis": "~2.2.0",
"cookie-parser": "~1.3.3",
"express-session": "~1.10.2",
"jade": "~1.9.1",
"method-override": "~2.3.1",
"morgan": "~1.5.1",
"multiparty": "~4.1.1",
"vhost": "~3.0.0"
},
"engines": {
"node": ">= 0.10.0"
},
"files": [
"LICENSE",
"History.md",
"Readme.md",
"index.js",
"lib/"
],
"scripts": {
"test": "mocha --require test/support/env
--reporter spec --bail --check-leaks test/ test/acceptance/",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha
-- --require test/support/env --reporter dot --check-leaks test/ test/acceptance/",
"test-tap": "mocha --require test/support/env
--reporter tap --check-leaks test/ test/acceptance/",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha
--report lcovonly -- --require test/support/env
--reporter spec --check-leaks test/ test/acceptance/"
},
"gitHead": "63ab25579bda70b4927a179b580a9c580b6c7ada",
"bugs": {
"url": "https://github.com/strongloop/express/issues"
},
"_id": "express@4.11.2",
"_shasum": "8df3d5a9ac848585f00a0777601823faecd3b148",
"_from": "express@*",
"_npmVersion": "1.4.28",
"_npmUser": {
"name": "dougwilson",
"email": "doug@somethingdoug.com"
},
"maintainers": [{
"name": "tjholowaychuk",
"email": "tj@vision-media.ca"
},
{
"name": "jongleberry",
"email": "jonathanrichardong@gmail.com"
},
{
"name": "shtylman",
"email": "shtylman@gmail.com"
},
{
"name": "dougwilson",
"email": "doug@somethingdoug.com"
},
{
"name": "aredridel",
"email": "aredridel@nbtsc.org"
},
{
"name": "strongloop",
"email": "callback@strongloop.com"
},
{
"name": "rfeng",
"email": "enjoyjava@gmail.com"
}],
"dist": {
"shasum": "8df3d5a9ac848585f00a0777601823faecd3b148",
"tarball": "https://registry.npmjs.org/express/-/express-4.11.2.tgz"
},
"directories": {},
"_resolved": "https://registry.npmjs.org/express/-/express-4.11.2.tgz",
"readme": "ERROR: No README data found!"
}
名称-包裹名称
版本-软件包的版本
描述-包装说明
主页-包的主页
作者-包的作者
贡献者-软件包贡献者的名称
依赖项–依赖项列表。 NPM会在软件包的node_module文件夹中自动安装此处提到的所有依赖项。
存储库-软件包的存储库类型和URL
main-包的入口点
关键字-关键字
使用以下命令来卸载Node.js模块。
$ npm uninstall express
NPM卸载软件包后,您可以通过查看/ node_modules /目录的内容或输入以下命令来对其进行验证-
$ npm ls
更新package.json并更改要更新的依赖项的版本,然后运行以下命令。
$ npm update express
使用NPM搜索软件包名称。
$ npm search express
创建模块需要生成package.json。让我们使用NPM生成package.json,这将生成package.json的基本框架。
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane defaults.
See 'npm help json' for definitive documentation on these fields
and exactly what they do.
Use 'npm install --save' afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
name: (webmaster)
您将需要提供有关模块的所有必需信息。您可以从上述package.json文件中获取帮助,以了解所需的各种信息的含义。生成package.json后,请使用以下命令使用有效的电子邮件地址在NPM存储库站点中进行注册。
$ npm adduser
Username: mcmohd
Password:
Email: (this IS public) mcmohd@gmail.com
现在是时候发布您的模块了-
$ npm publish
如果您的模块一切正常,那么它将像任何其他Node.js模块一样在存储库中发布,并且可以使用NPM进行安装。