模板安装-使用nmp命令




无标题

使用 npm 命令安装模块

npm 安装 Node.js 模块语法格式如下:

    
    
    
    
  1. $ npm install </span><span class="pun">&lt;</span><span class="typ">Module</span><span class="pln"> </span><span class="typ">Name</span><span class="pun">&gt;</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框架模块&nbsp;<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’) 的方式就好,无需指定第三方包路径。

    
    
    
    
  1. var express = require('express');

全局安装与本地安装

npm 的包安装分为本地安装(local)、全局安装(global)两种,从敲的命令行来看,差别只是有没有-g而已,比如

    
    
    
    
  1. npm install express # 本地安装
  2. npm install express -g # 全局安装

如果出现以下错误:

    
    
    
    
  1. npm err! Error: connect ECONNREFUSED 127.0.0.1:8087

解决办法为:

    
    
    
    
  1. $ 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

安装过程输出如下内容,第一行输出了模块的版本号及安装位置。


你可以使用以下命令来查看所有全局安装的模块:

    
    
    
    
  1. $ 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/ 目录下查看包是否还存在,或者使用以下命令查看:

    
    
    
    
  1. $ 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

搜索模块

使用以下来搜索模块:

    
    
    
    
  1. $ 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
  2. This utility will walk you through creating a package.json file.
  3. It only covers the most common items, and tries to guess sensible defaults.
  4. See </span><span class="pln">npm help json</span><span class="pun"> for definitive documentation on these fields
  5. and exactly what they do.
  6. Use </span><span class="pln">npm install </span><span class="pun">&lt;</span><span class="pln">pkg</span><span class="pun">&gt;</span><span class="pln"> </span><span class="pun">--</span><span class="pln">save</span><span class="pun"> afterwards to install a package and
  7. save it as a dependency in the package.json file.
  8. Press ^C at any time to quit.
  9. name: (node_modules) runoob # 模块名
  10. version: (1.0.0)
  11. description: Node.js 测试模块(www.runoob.com) # 描述
  12. entry point: (index.js)
  13. test command: make test
  14. git repository: https://github.com/runoob/runoob.git # Github 地址
  15. keywords:
  16. author:
  17. license: (ISC)
  18. About to write to ……/node_modules/package.json: # 生成地址
  19. {
  20. "name": "runoob",
  21. "version": "1.0.0",
  22. "description": "Node.js 测试模块(www.runoob.com)",
  23. ……
  24. }
  25. Is this ok? (yes) yes

以上的信息,你需要根据你自己的情况输入。在最后输入 “yes” 后会生成 package.json 文件。

接下来我们可以使用以下命令在 npm 资源库中注册用户(使用邮箱注册):

    
    
    
    
  1. $ 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文件

package.json 位于模块的目录下,用于定义包的属性。接下来让我们来看下 express 包的 package.json 文件,位于 node_modules/express/package.json 内容:

     
     
     
     
  1. {
  2. "name": "express",
  3. "description": "Fast, unopinionated, minimalist web framework",
  4. "version": "4.13.3",
  5. "author": {
  6. "name": "TJ Holowaychuk",
  7. "email": "[email protected]"
  8. },
  9. "contributors": [
  10. {
  11. "name": "Aaron Heckmann",
  12. "email": "[email protected]"
  13. },
  14. {
  15. "name": "Ciaran Jessup",
  16. "email": "[email protected]"
  17. },
  18. {
  19. "name": "Douglas Christopher Wilson",
  20. "email": "[email protected]"
  21. },
  22. {
  23. "name": "Guillermo Rauch",
  24. "email": "[email protected]"
  25. },
  26. {
  27. "name": "Jonathan Ong",
  28. "email": "[email protected]"
  29. },
  30. {
  31. "name": "Roman Shtylman",
  32. "email": "[email protected]"
  33. },
  34. {
  35. "name": "Young Jae Sim",
  36. "email": "[email protected]"
  37. }
  38. ],
  39. "license": "MIT",
  40. "repository": {
  41. "type": "git",
  42. "url": "git+https://github.com/strongloop/express.git"
  43. },
  44. "homepage": "http://expressjs.com/",
  45. "keywords": [
  46. "express",
  47. "framework",
  48. "sinatra",
  49. "web",
  50. "rest",
  51. "restful",
  52. "router",
  53. "app",
  54. "api"
  55. ],
  56. "dependencies": {
  57. "accepts": "~1.2.12",
  58. "array-flatten": "1.1.1",
  59. "content-disposition": "0.5.0",
  60. "content-type": "~1.0.1",
  61. "cookie": "0.1.3",
  62. "cookie-signature": "1.0.6",
  63. "debug": "~2.2.0",
  64. "depd": "~1.0.1",
  65. "escape-html": "1.0.2",
  66. "etag": "~1.7.0",
  67. "finalhandler": "0.4.0",
  68. "fresh": "0.3.0",
  69. "merge-descriptors": "1.0.0",
  70. "methods": "~1.1.1",
  71. "on-finished": "~2.3.0",
  72. "parseurl": "~1.3.0",
  73. "path-to-regexp": "0.1.7",
  74. "proxy-addr": "~1.0.8",
  75. "qs": "4.0.0",
  76. "range-parser": "~1.0.2",
  77. "send": "0.13.0",
  78. "serve-static": "~1.10.0",
  79. "type-is": "~1.6.6",
  80. "utils-merge": "1.0.0",
  81. "vary": "~1.0.1"
  82. },
  83. "devDependencies": {
  84. "after": "0.8.1",
  85. "ejs": "2.3.3",
  86. "istanbul": "0.3.17",
  87. "marked": "0.3.5",
  88. "mocha": "2.2.5",
  89. "should": "7.0.2",
  90. "supertest": "1.0.1",
  91. "body-parser": "~1.13.3",
  92. "connect-redis": "~2.4.1",
  93. "cookie-parser": "~1.3.5",
  94. "cookie-session": "~1.2.0",
  95. "express-session": "~1.11.3",
  96. "jade": "~1.11.0",
  97. "method-override": "~2.3.5",
  98. "morgan": "~1.6.1",
  99. "multiparty": "~4.1.2",
  100. "vhost": "~3.0.1"
  101. },
  102. "engines": {
  103. "node": ">= 0.10.0"
  104. },
  105. "files": [
  106. "LICENSE",
  107. "History.md",
  108. "Readme.md",
  109. "index.js",
  110. "lib/"
  111. ],
  112. "scripts": {
  113. "test": "mocha --require test/support/env --reporter spec --bail --check-leaks test/ test/acceptance/",
  114. "test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/ test/acceptance/",
  115. "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/ test/acceptance/",
  116. "test-tap": "mocha --require test/support/env --reporter tap --check-leaks test/ test/acceptance/"
  117. },
  118. "gitHead": "ef7ad681b245fba023843ce94f6bcb8e275bbb8e",
  119. "bugs": {
  120. "url": "https://github.com/strongloop/express/issues"
  121. },
  122. "_id": "[email protected]",
  123. "_shasum": "ddb2f1fb4502bf33598d2b032b037960ca6c80a3",
  124. "_from": "express@*",
  125. "_npmVersion": "1.4.28",
  126. "_npmUser": {
  127. "name": "dougwilson",
  128. "email": "[email protected]"
  129. },
  130. "maintainers": [
  131. {
  132. "name": "tjholowaychuk",
  133. "email": "[email protected]"
  134. },
  135. {
  136. "name": "jongleberry",
  137. "email": "[email protected]"
  138. },
  139. {
  140. "name": "dougwilson",
  141. "email": "[email protected]"
  142. },
  143. {
  144. "name": "rfeng",
  145. "email": "[email protected]"
  146. },
  147. {
  148. "name": "aredridel",
  149. "email": "[email protected]"
  150. },
  151. {
  152. "name": "strongloop",
  153. "email": "[email protected]"
  154. },
  155. {
  156. "name": "defunctzombie",
  157. "email": "[email protected]"
  158. }
  159. ],
  160. "dist": {
  161. "shasum": "ddb2f1fb4502bf33598d2b032b037960ca6c80a3",
  162. "tarball": "http://registry.npmjs.org/express/-/express-4.13.3.tgz"
  163. },
  164. "directories": {},
  165. "_resolved": "https://registry.npmjs.org/express/-/express-4.13.3.tgz",
  166. "readme": "ERROR: No README data found!"
  167. }


你可能感兴趣的:(node.js)