无标题
npm 安装 Node.js 模块语法格式如下:
$ npm install </span><span class="pun"><</span><span class="typ">Module</span><span class="pln"> </span><span class="typ">Name</span><span class="pun">></span></code></li></ol></pre></div><p style="border: 0px; padding: 0px; line-height: 2em; font-size: 12px; font-family: 'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif; color: rgb(51, 51, 51); widows: 1; background-color: rgb(255, 255, 255);">以下实例,我们使用 npm 命令安装常用的 Node.js web框架模块 <b>express</b></p><div><pre class="prettyprint linenums prettyprinted"><ol class="linenums"><li class="L0"><code class="language-js"><span class="pln">$ npm install express
安装好之后,express 包就放在了工程目录下的 node_modules 目录中,因此在代码中只需要通过 require(‘express’) 的方式就好,无需指定第三方包路径。
var express = require('express');
npm 的包安装分为本地安装(local)、全局安装(global)两种,从敲的命令行来看,差别只是有没有-g而已,比如
npm install express # 本地安装
npm install express -g # 全局安装
如果出现以下错误:
npm err! Error: connect ECONNREFUSED 127.0.0.1:8087
解决办法为:
$ npm config </span><span class="kwd">set</span><span class="pln"> proxy </span><span class="kwd">null</span></code></li></ol></pre></div><h3 style="border: 0px; margin-top: 8px; margin-bottom: 8px; font-size: 1.4em; color: rgb(51, 51, 51); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif; line-height: normal; widows: 1; background-color: rgb(255, 255, 255);">本地安装</h3><ul style="border: 0px; margin-top: 1em; margin-bottom: 1em; padding: 0px; line-height: 16.8px; list-style-type: none; color: rgb(51, 51, 51); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif; font-size: 12px; widows: 1; background-color: rgb(255, 255, 255);"><li style="border: 0px; margin-bottom: 1em; margin-left: 1em; padding: 0px 0px 0px 1.5em; line-height: 1.5em; font-size: 1em; background-image: url(file:///C:/Users/wxq/Documents/My%20Knowledge/temp/assets/img/bullet.png); background-position: 0% 0.5em; background-repeat: no-repeat no-repeat;">1. 将安装包放在 ./node_modules 下(运行 npm 命令时所在的目录),如果没有 node_modules 目录,会在当前执行 npm 命令的目录下生成 node_modules 目录。</li><li style="border: 0px; margin-bottom: 1em; margin-left: 1em; padding: 0px 0px 0px 1.5em; line-height: 1.5em; font-size: 1em; background-image: url(file:///C:/Users/wxq/Documents/My%20Knowledge/temp/assets/img/bullet.png); background-position: 0% 0.5em; background-repeat: no-repeat no-repeat;">2. 可以通过 require() 来引入本地安装的包。</li></ul><h3 style="border: 0px; margin-top: 8px; margin-bottom: 8px; font-size: 1.4em; color: rgb(51, 51, 51); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif; line-height: normal; widows: 1; background-color: rgb(255, 255, 255);">全局安装</h3><ul style="border: 0px; margin-top: 1em; margin-bottom: 1em; padding: 0px; line-height: 16.8px; list-style-type: none; color: rgb(51, 51, 51); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif; font-size: 12px; widows: 1; background-color: rgb(255, 255, 255);"><li style="border: 0px; margin-bottom: 1em; margin-left: 1em; padding: 0px 0px 0px 1.5em; line-height: 1.5em; font-size: 1em; background-image: url(file:///C:/Users/wxq/Documents/My%20Knowledge/temp/assets/img/bullet.png); background-position: 0% 0.5em; background-repeat: no-repeat no-repeat;">1. 将安装包放在 /usr/local 下。</li><li style="border: 0px; margin-bottom: 1em; margin-left: 1em; padding: 0px 0px 0px 1.5em; line-height: 1.5em; font-size: 1em; background-image: url(file:///C:/Users/wxq/Documents/My%20Knowledge/temp/assets/img/bullet.png); background-position: 0% 0.5em; background-repeat: no-repeat no-repeat;">2. 可以直接在命令行里使用。</li><li style="border: 0px; margin-bottom: 1em; margin-left: 1em; padding: 0px 0px 0px 1.5em; line-height: 1.5em; font-size: 1em; background-image: url(file:///C:/Users/wxq/Documents/My%20Knowledge/temp/assets/img/bullet.png); background-position: 0% 0.5em; background-repeat: no-repeat no-repeat;">3. 不能通过 require() 来引入本地安装的包。</li></ul><p style="border: 0px; padding: 0px; line-height: 2em; font-size: 12px; font-family: 'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif; color: rgb(51, 51, 51); widows: 1; background-color: rgb(255, 255, 255);">接下来我们使用全局方式安装 express</p><div><pre class="prettyprint linenums prettyprinted"><ol class="linenums"><li class="L0"><code class="language-js"><span class="pln">$ npm install express -g
安装过程输出如下内容,第一行输出了模块的版本号及安装位置。
你可以使用以下命令来查看所有全局安装的模块:
$ npm ls </span><span class="pun">-</span><span class="pln">g</span></code></li></ol></pre></div><div><h2 style="border: 0px; margin-top: 2px; margin-bottom: 2px; font-size: 1.8em; line-height: 1.8em; color: rgb(51, 51, 51); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif; widows: 1; background-color: rgb(255, 255, 255);">卸载模块</h2><p style="border: 0px; padding: 0px; line-height: 2em; font-size: 12px; font-family: 'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif; color: rgb(51, 51, 51); widows: 1; background-color: rgb(255, 255, 255);">我们可以使用以下命令来卸载 Node.js 模块。</p><div><pre class="prettyprint linenums prettyprinted"><ol class="linenums"><li class="L0"><code class="language-js"><span class="pln">$ npm uninstall express
卸载后,你可以到 /node_modules/ 目录下查看包是否还存在,或者使用以下命令查看:
$ npm ls</span></code></li></ol></pre></div><hr style="color: rgb(212, 212, 212); height: 1px; border: 0px; clear: both; font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif; font-size: 12px; line-height: normal; widows: 1; background-color: rgb(212, 212, 212);"><h2 style="border: 0px; margin-top: 2px; margin-bottom: 2px; font-size: 1.8em; line-height: 1.8em; color: rgb(51, 51, 51); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif; widows: 1; background-color: rgb(255, 255, 255);">更新模块</h2><p style="border: 0px; padding: 0px; line-height: 2em; font-size: 12px; font-family: 'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif; color: rgb(51, 51, 51); widows: 1; background-color: rgb(255, 255, 255);">我们可以使用以下命令更新模块:</p><div><pre class="prettyprint linenums prettyprinted"><ol class="linenums"><li class="L0"><code class="language-js"><span class="pln">$ npm update express
使用以下来搜索模块:
$ npm search express</span></code></li></ol></pre></div><hr style="color: rgb(212, 212, 212); height: 1px; border: 0px; clear: both; font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif; font-size: 12px; line-height: normal; widows: 1; background-color: rgb(212, 212, 212);"><h2 style="border: 0px; margin-top: 2px; margin-bottom: 2px; font-size: 1.8em; line-height: 1.8em; color: rgb(51, 51, 51); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif; widows: 1; background-color: rgb(255, 255, 255);">创建模块</h2><p style="border: 0px; padding: 0px; line-height: 2em; font-size: 12px; font-family: 'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif; color: rgb(51, 51, 51); widows: 1; background-color: rgb(255, 255, 255);">创建模块,package.json 文件是必不可少的。我们可以使用 NPM 生成 package.json 文件,生成的文件包含了基本的结果。</p><div><pre class="prettyprint linenums prettyprinted"><ol class="linenums"><li class="L0"><code class="language-js"><span class="pln">$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See
</span><span class="pln">npm help json</span><span class="pun">
for definitive documentation on these fieldsand exactly what they do.
Use
</span><span class="pln">npm install </span><span class="pun"><</span><span class="pln">pkg</span><span class="pun">></span><span class="pln"> </span><span class="pun">--</span><span class="pln">save</span><span class="pun">
afterwards to install a package andsave it as a dependency in the package.json file.
Press ^C at any time to quit.
name: (node_modules) runoob # 模块名
version: (1.0.0)
description: Node.js 测试模块(www.runoob.com) # 描述
entry point: (index.js)
test command: make test
git repository: https://github.com/runoob/runoob.git # Github 地址
keywords:
author:
license: (ISC)
About to write to ……/node_modules/package.json: # 生成地址
{
"name": "runoob",
"version": "1.0.0",
"description": "Node.js 测试模块(www.runoob.com)",
……
}
Is this ok? (yes) yes
以上的信息,你需要根据你自己的情况输入。在最后输入 “yes” 后会生成 package.json 文件。
接下来我们可以使用以下命令在 npm 资源库中注册用户(使用邮箱注册):
$ npm adduser</span></code></li><li class="L1"><code class="language-js"><span class="typ">Username</span><span class="pun">:</span><span class="pln"> mcmohd</span></code></li><li class="L2"><code class="language-js"><span class="typ">Password</span><span class="pun">:</span><span class="typ">Email</span><span class="pun">:</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">this</span><span class="pln"> IS </span><span class="kwd">public</span><span class="pun">)</span><span class="pln"> mcmohd@gmail</span><span class="pun">.</span><span class="pln">com</span></code></li></ol></pre></div><div><br></div><p style="border: 0px; padding: 0px; line-height: 2em; font-size: 12px; font-family: 'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif; color: rgb(51, 51, 51); widows: 1; background-color: rgb(255, 255, 255);">接下来我们就用以下命令来发布模块:</p><div><pre class="prettyprint linenums prettyprinted"><ol class="linenums"><li class="L0"><code class="language-js"><span class="pln">$ npm publish
package.json 位于模块的目录下,用于定义包的属性。接下来让我们来看下 express 包的 package.json 文件,位于 node_modules/express/package.json 内容:
{
"name": "express",
"description": "Fast, unopinionated, minimalist web framework",
"version": "4.13.3",
"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": "git+https://github.com/strongloop/express.git"
},
"homepage": "http://expressjs.com/",
"keywords": [
"express",
"framework",
"sinatra",
"web",
"rest",
"restful",
"router",
"app",
"api"
],
"dependencies": {
"accepts": "~1.2.12",
"array-flatten": "1.1.1",
"content-disposition": "0.5.0",
"content-type": "~1.0.1",
"cookie": "0.1.3",
"cookie-signature": "1.0.6",
"debug": "~2.2.0",
"depd": "~1.0.1",
"escape-html": "1.0.2",
"etag": "~1.7.0",
"finalhandler": "0.4.0",
"fresh": "0.3.0",
"merge-descriptors": "1.0.0",
"methods": "~1.1.1",
"on-finished": "~2.3.0",
"parseurl": "~1.3.0",
"path-to-regexp": "0.1.7",
"proxy-addr": "~1.0.8",
"qs": "4.0.0",
"range-parser": "~1.0.2",
"send": "0.13.0",
"serve-static": "~1.10.0",
"type-is": "~1.6.6",
"utils-merge": "1.0.0",
"vary": "~1.0.1"
},
"devDependencies": {
"after": "0.8.1",
"ejs": "2.3.3",
"istanbul": "0.3.17",
"marked": "0.3.5",
"mocha": "2.2.5",
"should": "7.0.2",
"supertest": "1.0.1",
"body-parser": "~1.13.3",
"connect-redis": "~2.4.1",
"cookie-parser": "~1.3.5",
"cookie-session": "~1.2.0",
"express-session": "~1.11.3",
"jade": "~1.11.0",
"method-override": "~2.3.5",
"morgan": "~1.6.1",
"multiparty": "~4.1.2",
"vhost": "~3.0.1"
},
"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-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --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/"
},
"gitHead": "ef7ad681b245fba023843ce94f6bcb8e275bbb8e",
"bugs": {
"url": "https://github.com/strongloop/express/issues"
},
"_id": "[email protected]",
"_shasum": "ddb2f1fb4502bf33598d2b032b037960ca6c80a3",
"_from": "express@*",
"_npmVersion": "1.4.28",
"_npmUser": {
"name": "dougwilson",
"email": "[email protected]"
},
"maintainers": [
{
"name": "tjholowaychuk",
"email": "[email protected]"
},
{
"name": "jongleberry",
"email": "[email protected]"
},
{
"name": "dougwilson",
"email": "[email protected]"
},
{
"name": "rfeng",
"email": "[email protected]"
},
{
"name": "aredridel",
"email": "[email protected]"
},
{
"name": "strongloop",
"email": "[email protected]"
},
{
"name": "defunctzombie",
"email": "[email protected]"
}
],
"dist": {
"shasum": "ddb2f1fb4502bf33598d2b032b037960ca6c80a3",
"tarball": "http://registry.npmjs.org/express/-/express-4.13.3.tgz"
},
"directories": {},
"_resolved": "https://registry.npmjs.org/express/-/express-4.13.3.tgz",
"readme": "ERROR: No README data found!"
}