Node.js npm - Node.js教程 - PHPERZ中文资讯站 - 胜博发888手机平台

发布于 2015-07-05 20:30:53 | 440 次阅读 | 评论: 0 | 来源: 网络整理

Node程序包管理器(NPM)提供了以下两个主要功能:

  • 在线存储库的Node.js包/模块,可搜索 search.nodejs.org

  • 命令行实用程序来安装Node.js的包,做版本管理和Node.js包依赖管理。

NPM捆绑v0.6.3版本在一起以后,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
[email protected] /usr/lib/node_modules/npm

使用NPM安装模块

有一个简单安装任何Node.js模块,语法如下:


$ npm install <Module Name>

例如,下面是安装一个著名的Node.jsweb框架模块的命令叫 express:


$ npm install express

现在,你可以在js文件中使用此模块如下:


var express = require('express');

全局VS本地安装

默 认情况下,NPM安装任何依赖在本地模式。在这里,本地模式是指包安装在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(命令行界面)功能可以使用,但不能直接使用require()的Node应用程序中导入。 现在,让我们尝试使用全局安装Express模块。


$ npm install express -g

这将产生类似的结果,模块将在全局范围内安装。在这里,第一行讲述了在那里得到安装模块版本和它的位置。


[email protected] /usr/lib/node_modules/express
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
└── [email protected] ([email protected], [email protected])

您可以使用下面的命令来检查所有全局安装的模块:


$ npm ls -g

使用package.json

package.json是存在于任何Node应用程序/模块的根目录和用于定义一个包的属性。让我们打开package.json在当前 node_modules/express/


{
  "name": "express",
  "description": "Fast, unopinionated, minimalist web framework",
  "version": "4.11.2",
  "author": {
    "name": "TJ Holowaychuk",
    "email": "[email protected]"
  },
  "contributors": [
    {
      "name": "Aaron Heckmann",
      "email": "[email protected]"
    },
    {
      "name": "Ciaran Jessup",
      "email": "[email protected]"
    },
    {
      "name": "Douglas Christopher Wilson",
      "email": "[email protected]"
    },
    {
      "name": "Guillermo Rauch",
      "email": "[email protected]"
    },
    {
      "name": "Jonathan Ong",
      "email": "[email protected]"
    },
    {
      "name": "Roman Shtylman",
      "email": "[email protected]"
    },
    {
      "name": "Young Jae Sim",
      "email": "[email protected]"
    }
  ],
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "https://github.com/strongloop/express"
  },
  "homepage": "http://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": "[email protected]",
  "_shasum": "8df3d5a9ac848585f00a0777601823faecd3b148",
  "_from": "[email protected]*",
  "_npmVersion": "1.4.28",
  "_npmUser": {
    "name": "dougwilson",
    "email": "[email protected]"
  },
  "maintainers": [
    {
      "name": "tjholowaychuk",
      "email": "[email protected]"
    },
    {
      "name": "jongleberry",
      "email": "[email protected]"
    },
    {
      "name": "shtylman",
      "email": "[email protected]"
    },
    {
      "name": "dougwilson",
      "email": "[email protected]g.com"
    },
    {
      "name": "aredridel",
      "email": "[email protected]"
    },
    {
      "name": "strongloop",
      "email": "[email protected]"
    },
    {
      "name": "rfeng",
      "email": "[email protected]"
    }
  ],
  "dist": {
    "shasum": "8df3d5a9ac848585f00a0777601823faecd3b148",
    "tarball": "http://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!"
}

Package.json的属性

  • name - 包的名称

  • version - 包的版本

  • description - 包的描述

  • homepage - 包主页

  • author - 包的作者

  • contributors - 贡献者到包的名字

  • dependencies - 依赖关系的列表。NPM自动安装所有在这里的包node_module文件夹中提到的依赖关系。

  • repository - 包的库类型和URL

  • main - 包的入口点

  • keywords - 关键字

卸载模块

使用下面的命令卸载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 <pkg> --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库胜博发888手机平台上注册自己信息。


$ npm adduser
Username: mcmohd
Password:
Email: (this IS public) [email protected]

现在,发布你的模块的胜博發:


$ npm publish

如果你的模块正常使用,然后将它发表在库中并且能够使用NPM就像任何其他其他Node.js的模块安装。

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  服务